我有两个型号:(专辑和产品)
1)内部模型
在album.rb里面:
class Album < ActiveRecord::Base
attr_accessible :name
has_many :products
end
Run Code Online (Sandbox Code Playgroud)
内部product.rb:
class Product < ActiveRecord::Base
attr_accessible :img, :name, :price, :quantity
belongs_to :album
end
Run Code Online (Sandbox Code Playgroud)
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?
Run Code Online (Sandbox Code Playgroud)