I have two models: (Albums and Product)
1) Inside Models
Inside album.rb:
class Album < ActiveRecord::Base
attr_accessible :name
has_many :products
end
Inside product.rb:
class Product < ActiveRecord::Base
attr_accessible :img, :name, :price, :quantity
belongs_to :album
end
2) Using "rails console", how can I set the associations (so I can use "<%= Product.first.album.name %>")?
e.g.
a = Album.create( :name => "My Album" )
p = Product.create( :name => "Shampoo X" )
# what's next? how can i set the album and the product together?
_id
to accessible, then you can just stick the id value as you instantiate it:Product.create(name:'Shampoo',album_id:a.id)
– Expedition