class Item < ActiveRecord::Base
validates :title, presence: true
has_many :items_categories
has_many :categories, through: :items_categories
has_many :item_orders
has_many :orders, through: :item_orders
def quantity
end
end
我的问题是为什么我们需要这些线:
has_many :items_categories
和
has_many :items_oders
他们似乎多余。我们为什么需要它们?
为了澄清,项目和类别是多对多的关系。 物品和订单是多对多的关系。
答案 0 :(得分:0)
items_categories
和item_orders
是与Item完全成熟的关联。 Item类需要知道它们以便通过&#34;查看&#34;他们找到类别和订单。
它们并不多余 - 你为什么认为它们是?通常,您最终会创建和删除联接记录,例如通过items_categories_controller或item_orders_controller,以便将现有项目与现有订单/类别相关联/取消关联。