我有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
记录插入每个产品列中,即在{{1}中记录10条Price
条记录} {,:a4_single
中有8个,:a4_double
中有2个,:a3_double
中有8个。
我只是猜测上面定义的:two_a3_double
关系是正确的,我真的不知道如何从这里开始。
答案 0 :(得分:2)
你不应该继续下去。
做这样的事情
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