3
如何更新所有產品以將特定用戶分配給他們?播種時可以更新我的所有產品給特定用戶嗎?
admin = User.create(:name => "Admin", :password => "password")
walmart = Store.create(:name => 'Walmart', :address => 'San Francisco, Palo Alto')
walmartprices = walmart.products.create
([
{:name => "Rice", :price => '5.93'},
{:name => "Chicken", :price => "2.24"},
{:name => "Milk", :price => '3.81'},
{:name => 'Eggs', :price => '2.78'}
])
walmartprices.update_attribute(:user => admin)
當然,這給了我一個錯誤:
undefined method `update_attribute' for #<Array:0x5342f70>
這可能嗎?如何做呢?
編輯
這是我協會:
Product
belongs_to :user and :store
Store
has_many :products
User
has_many :products
通過外觀'walmartprices'將是一個數組'walmart.products.create'方法接受一個散列數組。 我不能告訴用戶如何適應這裏,用戶如何關聯? – Bangline 2012-01-09 11:31:03
@Bangline它與產品相關聯。 – LearningRoR 2012-01-09 16:28:23
我走了你使用mr_x方法然後..或 walmartprices = walmart.products.create ([{ :名稱=> 「水稻」,:價格=> '5.93',:用戶=>管理員},{ : name:>「Chicken」,:price =>「2.24」,:user => admin}, {:name =>「Milk」,:price =>'3.81',:user => admin}, {:name = name =>'Eggs',:price =>'2.78',:user => admin} ]) – Bangline 2012-01-09 16:52:31