So here is a object I retrieved from the ActiveRecord other = [#<Referrer id: 16, name: "Other", published: true, created_at: "2011-07-11 16:22:00", updated_at: "2011-07-11 16:22:00">]
Why can’t I do other.created_at while I can do other.name?
Here is the model:
class Referrer < ActiveRecord::Base
end
The error I’m getting:
NoMethodError: undefined method `created_at' for #<ActiveRecord::Relation:0x1035763c8>
What have I missed?
If you’ll notice,
otheris actually anActiveRecord::Relationwhich basically means it’s acting like an array ofReferrerobjects. So you can’t just callcreated_aton it. You could callother.first.created_ator if you want an array ofcreated_atdates you could doother.map(&:created_at).The reason you can call
other.nameis because anActiveRecord::Relationresponds to name. But it’s a differentnamemethod than thenameattribute on yourReferrermodel.other.nameshould return"Referrer"instead of"Other"right?