我有两个模型:(专辑和产品)
1) 内部模型
里面的专辑.rb:
class Album < ActiveRecord::Base
attr_accessible :name
has_many :products
end
product.rb 内部:
class Product < ActiveRecord::Base
attr_accessible :img, :name, :price, :quantity
belongs_to :album
end
2)使用“ rails console ”,我如何设置关联(所以我可以使用“<%= Product.first.album.name %>”)?
例如
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?