【发布时间】:2019-03-23 11:47:04
【问题描述】:
我有两个模型:Category 和 Subcategory
我可能会遇到 Subcategory 将包含更多 Subcategories
的情况如何使用 rails 关联来做到这一点?
现在我的代码:
category.rb
class Category < ApplicationRecord
has_many :subcategories, :dependent => :destroy
end
subcategory.rb
class Subcategory < ApplicationRecord
belongs_to :category
has_many :products, :dependent => :destroy
end
可能的例子:
Category 可读 --> Subcategory 书籍 --> Subcategory 儿童书籍 --> 产品 p>
【问题讨论】:
-
儿童读物只能在一个子类别下,还是可以在一个类别下?像
CategoryReadable -->SubcategoryBooks for kids 这种情况可能吗?
标签: ruby-on-rails postgresql migration associations activemodel