我可以在播种时将所有产品更新到特定用户吗?

如何更新所有产品以将特定用户分配给他们?

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 # 

这可能吗? 怎么做到呢?


编辑

这些是我的协会:

 Product belongs_to :user and :store Store has_many :products User has_many :products 

一个天真的方法是:

 walmartprices.each{|record| record.update_attribute(user: admin) } 

为了获得更好的性能,请使用update_all :

 Product.where( id: walmartprices.map(&:id) ).update_all( user: admin )