我觉得这是一个简单的问题,由于我对新的ActiveRecord查询接口的误解,但以此为例:Rails 3,Active Record查询返回ActiveRecord :: Relation对象,而不是对象
>> Category.first.recipes
=> [ ... ] # array of recipes
但是:
>> Category.where(:id => 1).recipes
=> NoMethodError: undefined method `recipes' for #<ActiveRecord::Relation:0x000001033dc9e0>
这是怎么回事吗?为什么我的where
方法返回一个ActiveRecord::Relation
对象?我该如何从查询中检索对象?
投下来,因为它不等效。 'where'返回ActiveRecord :: Relation,其余返回Array或Model.class – 2010-12-26 12:20:02
它在上下文中是等价的。这就是为什么我提到懒加载。但是,“.where.all”是等同的。 – Swanand 2010-12-27 05:02:55