Rails向现有的一对多关联中添加了第三种模型

时间:2018-07-17 02:23:25

标签: ruby-on-rails activerecord

我目前具有以下模型关联:

旅行

has_many :items

项目

belongs_to :trip


我想在第三个模型之间引入第三个模型,称为item_collection,例如:

旅行

has_many :item_collections

has_many :items, through: :item_collections

Item_Collection

belongs_to :trip

has_many :items

项目

belongs_to :item_collection

belongs_to :trip


我对此有一些疑问:

  • 行程模型中是否可以有has_many :items, through: :item_collections?这有用还是只是多余?
  • 项目模型中是否可以有belongs_to :trip?还是我必须在项目表中删除跳闸的现有参考/外键?
  • 要建立这种新关系,我需要通过现有数据库中的ItemsTrips链接到他们的ItemCollection。首先,我计划将所有Items放在一个ItemCollection中。用各自的信息填充ItemCollectionTripItem表的最佳方法是什么? SQL?

感谢您的帮助!

0 个答案:

没有答案