ruby on rails 3 - has_many-relationship列中的许多模型记录



我有一个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

相关内容

  • 没有找到相关文章

最新更新