我有一个奇怪的问题,我有带型号的桌子产品
class Product < ActiveRecord::Base
attr_accessible :id, :category_id, :name, :barcode, :price, ...
但是当我运行rails c时,我无法访问属性。
product = Product.where("barcode='B0000000008'")
Product Load (22.5ms) EXEC sp_executesql N'SELECT [products].* FROM [products] WHERE (barcode=''B0000000008'')'
=> [#<Product id: 8, category_id: 2, name: "Aplikovaná boj. umění (1 hodina)", barcode: "P0000000008", price: #<BigDecimal:362f9c8,'0.95E2',9(36)>, ... ]
>> product.name
=> "Product"
>> product.class
=> ActiveRecord::Relation
>> product.barcode
!! #<NoMethodError: undefined method `barcode' for #<ActiveRecord::Relation:0x00000003a354c8>>
>> product.id
!! #<NoMethodError: undefined method `id' for #<ActiveRecord::Relation:0x00000003a354c8>>
>> product.price
!! #<NoMethodError: undefined method `price' for #<ActiveRecord::Relation:0x00000003a354c8>>
但我能跑
>> product = Product.new
>> product.name = "xx"
=> "xx"
>> product.class
=> Product(id: integer, ...)
Product 类和 ActiveRecord::Relation 类有什么区别?我如何从 where 方法中获取 Product 类?谢谢