0
查询返回基于使用限制()或第一不同的对象类型AR查询返回的关系,而不是
此示例使用限制(1),并且不产生预期的对象类型的预期对象类型(模型人) :
c = PersonCategory.where("category = ?", "Crown").limit(1) ##
=> PersonCategory Load (0.3ms) SELECT `person_categories`.* FROM `person_categories` WHERE (category = 'Crown') LIMIT 1
=> [#<PersonCategory id: 1, category: "Crown">] #####
c.class
=> ActiveRecord::Relation
本例使用第一和得到所需的输出:
c = PersonCategory.where("category = ?", "Crown").first ##
=> PersonCategory Load (0.4ms) SELECT `person_categories`.* FROM `person_categories` WHERE (category = 'Crown') LIMIT 1
=> #<PersonCategory id: 1, category: "Crown">
c.class
=> PersonCategory(id: integer, category: string) #####
ruby-1.9.2-p180 :034 >
当然!!!谢谢。 –