[英]Rails join table record creation
如果我有一個稱為:inventory_items_shopping_lists
的HABTM連接表,如何在該表中創建和調用記錄? 我沒有加入模型。 兩種加入的模型是:inventory_items
和:shopping_lists
。 我希望用戶可以將:inventory_items
添加到他們的:shopping_lists
。 提前致謝!
編輯
以下是相關的模型和我的目標:
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
我試圖讓:user
將:inventory_items
添加到:shopping_list
,它們將變為:list_items
。 list_items
具有:inventory_items
幾乎相同的屬性,因此我可能只是通過關聯引用這些屬性,而不是通過復制:list_items
表中的屬性來引用。 有什么想法或建議嗎? 我是RoR的新手,因此,感謝您對此計划任何部分的反饋。 謝謝!
如何在此表中創建和調用記錄?
您基本上沒有使用HABTM關聯類型。 如果要在連接表上存儲更多狀態,則需要使用標准的has_many
(可能還有has_many :through
),以便可以操縱基礎連接模型。
因此,我從來都不是HABTM的粉絲,實際上,我在生產中有一些大型的Rails應用程序,而且我自己從未使用過HABTM。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.