0

我有一个Brand模型和一个Price模型,因此:

品牌.rb

class Brand < ActiveRecord::Base
  attr_accessible :name, :a4_single, :a4_double, :a3_double, :two_a3_double
  has_many :prices, :dependent => :destroy
end

价格.rb

class Price < Brand
  attr_accessible :type, :quantity, :price, :brand_id
  belongs_to :brand
end

我希望能够在每个产品列中插入 Price条记录,例如 10条记录 in 、8 条 in 、2条 in和 8 条 in 。Price:a4_single:a4_double:a3_double:two_a3_double

我只是猜测has_many上面定义的关系是正确的,我真的不知道如何从这里着手。

4

1 回答 1

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
于 2013-01-16T05:25:32.363 回答