[英]Rails join table record creation
If I have a HABTM join table called, :inventory_items_shopping_lists
, how do I create and call records in this table? 如果我有一个称为
:inventory_items_shopping_lists
的HABTM连接表,如何在该表中创建和调用记录? I do not have a join model. 我没有加入模型。 The two joined models are
:inventory_items
and :shopping_lists
. 两种加入的模型是
:inventory_items
和:shopping_lists
。 I'd like to be able to have a user add :inventory_items
to their :shopping_lists
. 我希望用户可以将
:inventory_items
添加到他们的:shopping_lists
。 Thanks in advance! 提前致谢!
EDIT 编辑
Here are the relevant models and my goal: 以下是相关的模型和我的目标:
class InventoryItem < ActiveRecord::Base
belongs_to :item, :foreign_key => :item_id
belongs_to :vendor
has_many :list_items
end
class ListItem < ActiveRecord::Base
belongs_to :inventory_item, :foreign_key => :item_id
belongs_to :shopping_list
end
class ShoppingList < ActiveRecord::Base
has_many :list_items
belongs_to :user, :foreign_key => :user_id
end
I'm attempting to have a :user
add :inventory_items
to a :shopping_list
where they will become :list_items
. 我试图让
:user
将:inventory_items
添加到:shopping_list
,它们将变为:list_items
。 list_items
have almost identical attributes of :inventory_items
, so I will likely just reference those attributes through association than by duplicating the attributes in the :list_items
table. list_items
具有:inventory_items
几乎相同的属性,因此我可能只是通过关联引用这些属性,而不是通过复制:list_items
表中的属性来引用。 Thoughts or suggestions on this? 有什么想法或建议吗? I'm newish to RoR so I appreciate any feedback on any part of this plan.
我是RoR的新手,因此,感谢您对此计划任何部分的反馈。 Thanks!
谢谢!
how do I create and call records in this table?
如何在此表中创建和调用记录?
You basically don't with a HABTM association type. 您基本上没有使用HABTM关联类型。 If you want to store more state on the join table than you will need to use a standard
has_many
(and maybe a has_many :through
) so you can manipulate the underlying join model. 如果要在连接表上存储更多状态,则需要使用标准的
has_many
(可能还有has_many :through
),以便可以操纵基础连接模型。
For this reason I have never been a fan of HABTM and in fact I have a handful of large Rails apps in production and I have never used HABTM myself. 因此,我从来都不是HABTM的粉丝,实际上,我在生产中有一些大型的Rails应用程序,而且我自己从未使用过HABTM。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.