Guest User

Untitled

a guest
Aug 7th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. Can I update all of my products to a specific user when seeding?
  2. admin = User.create(:name => "Admin", :password => "password")
  3.  
  4. walmart = Store.create(:name => 'Walmart', :address => 'San Francisco, Palo Alto')
  5.  
  6. walmartprices = walmart.products.create
  7. ([
  8. {:name => "Rice", :price => '5.93'},
  9. {:name => "Chicken", :price => "2.24"},
  10. {:name => "Milk", :price => '3.81'},
  11. {:name => 'Eggs', :price => '2.78'}
  12. ])
  13.  
  14. walmartprices.update_attribute(:user => admin)
  15.  
  16. undefined method `update_attribute' for #<Array:0x5342f70>
  17.  
  18. Product
  19. belongs_to :user and :store
  20.  
  21. Store
  22. has_many :products
  23.  
  24. User
  25. has_many :products
  26.  
  27. walmartprices.each{|record| record.update_attribute(user: admin) }
  28.  
  29. Product.where( id: walmartprices.map(&:id) ).update_all( user: admin )
Add Comment
Please, Sign In to add comment