我有一个Brand
模型和一个Price
模型,因此:
brand.rb
class Brand < ActiveRecord::Base
attr_accessible :name, :a4_single, :a4_double, :a3_double, :two_a3_double
has_many :prices, :dependent => :destroy
end
price.rb
class Price < Brand
attr_accessible :type, :quantity, :price, :brand_id
belongs_to :brand
end
我希望能够插入多个 Price
记录到每个产品列-即例如,:a4_single
中有10条Price
记录,:a4_double
中有8条,:a3_double
中有2条,:two_a3_double
中有8条。
我只是猜测上面定义的has_many
关系是正确的,我真的不知道如何从这里开始。
你不应该再继续了。
像这样做
class Brand < ActiveRecord::Base
has_many :brand_prices
has_many :prices, :through => :brand_prices
attr_accessible :name
end
class Price < ActiveRecord::Base
has_many :brand_prices
has_many :brands, :through => :brand_prices
attr_accessible :price, :quantity, :type
end
class BrandPrice < ActiveRecord::Base
belongs_to :brand
belongs_to :price
end