许多模型记录在has_many-relationship列中

时间:2013-01-16 03:41:56

标签: ruby-on-rails-3 has-many belongs-to

我有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关系是正确的,我真的不知道如何从这里开始。

1 个答案:

答案 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