I have a Position model for which I have a scope defined:
scope :default, where('is_default = ?', 1)
Idea being that I want to know which is the default position. I can do something like: @profile.positions.default and this returns an activerecord relation and the default position record. The issue is that now that I have the default record, I need to access other attributes of Positions such as title..
@profile.positions.default.title
but the above returns an error: NoMethodError: undefined method `title’ for #
Any clues? Thanks.
A scope turns a
collectionof objects, not a single object, so you’re trying to calltitleon an array of ActiveRecord results.You probably want something like this:
Or if you always want just one record, you may switch from a scope to a class method: