0
试图在我的应用程序中具有以下关系。产品和类别关系
产品可以属于许多类别,子类别和子类别。
目前的设计:
Product:
has_many :categorizations, dependent: :destroy
has_many :categories, through: :categorizations
has_many :sub_categories, through: :categorizations
has_many :sub_sub_categories, through: :categorizations
Category:
has_many :categorizations
has_many :products, through: :categorizations
has_many :sub_categories, class_name: 'Category', foreign_key: 'parent_id'
belongs_to :parent_category, class_name: 'Category', foreign_key: 'parent_id'
Categorization:
belongs_to :category
belongs_to :sub_category, class_name: 'Category', foreign_key: 'sub_category_id'
belongs_to :sub_sub_category, class_name: 'Category', foreign_key: 'sub_sub_category_id'
belongs_to :product
产品特定类别的可以被列为category.products
。
如何访问特定产品sub_category
和sub_sub_category
?
我应该做些什么?
谢谢。和上面的设计是罚款或使用自引用关系在'category'而不是'sub_category'更好? – rAzOr
看看我编辑的答案@rAzOr – fongfan999