NoMethodError:在Rails中未定义的方法`created_at’

所以这是我从ActiveRecord中检索到的other = [#]对象other = [#]为什么我不能做other.created_at而我可以做other.name

这是模型:

 class Referrer < ActiveRecord::Base end 

我得到的错误:

 NoMethodError: undefined method `created_at' for # 

我错过了什么?

如果您注意到, other实际上是ActiveRecord::Relation ,这基本上意味着它就像一个Referrer对象数组。 所以你不能只在它上面调用created_at 。 您可以调用other.first.created_at或者如果您想要一个created_at日期数组,您可以执行other.map(&:created_at)

你可以调用other.name的原因是因为ActiveRecord::Relation响应了name。 但它是与Referrer模型上的name属性不同的name方法。 other.name应该返回"Referrer"而不是"Other"对吗?