有争议的是,HABTM 很少(如果有的话)是一个好的设计,而 IMO 几乎是 Rails 唯一出错的地方。
引入一个外部参照表来连接产品和类别,并在关系的两边使用 has_many :through ,所以你最终得到
class Brand < ActiveRecord::Base
has_many :products
has_many categories :through => products # This is now allowed in Rails 3.x and above
end
class Category < ActiveRecord::Base
belongs_to :product_category
has_many :products :through => product_category
end
class Product < ActiveRecord::Base
belongs_to :brand
belongs_to :product_category
has_many :categories :through => product_category
end
class ProductCategory < ActiveRecord::Base
has_many :products
has_many :categories
end
这为您提供了最大的灵活性,为您提供了最少的代码重构以及更直观的路径来获取关系任一方所需的任何数据,并使您能够实现以下目标
b = Brand.find(1)
b.categories.all
更新
上面是完全未经测试的代码,我刚刚纠正了我犯的一个明显愚蠢的错误。如果您在实施此操作时遇到任何问题,请返回