![](/img/trans.png)
[英]Unpermitted parameter error in creating new model with has_many through association in Rails
[英]Creating a Restaurant Menu should I use a has_many through or habtm association?
我正在嘗試創建一個Restaurant項目並設計菜單的數據庫。 在創建數據庫模式時,我似乎一直在努力探索如何在以下模型MenuItem, Menu, and a Menu Section
之間實現正確的關聯。 每次我通過has_many :through
和has_and_belongs_to_many
,我都能看到兩者的好處,但是當我實現它們時,我會感到困惑。 另外,我對混合的self join
感到困惑,這進一步使我失望。
我認為我的工作量過大,但是對數據庫架構的一些輸入會有所幫助。 更具體地說,您認為以下情況我應使用哪種關聯?
到目前為止,我有三個類/表Restaurant, Menu, and MenuItem
以及我Restaurant, Menu, and MenuItem
的方案:
class Restaurant
has_many :menus (#Desert, Drinks, Dinner, Lunch, etc.)
end
class Menu
has_many :sections, class_name: "Menu", foreign_key: "sections_id"
belongs_to :section, class_name: "Menu"
has_many :menu_items, through: :sections
belongs_to :restaurant
end
## Context for the Menu and Sections
If I had a drinks menu with the following sections, Non Alcoholic,
Spirits, Wine, etc. These 'sections' are essentially menus with
their own menu items. This is why I put a self join on the Menu
class. Maybe Im mistaken.
class MenuItem
belongs_to :section, class_name: "Menu"
belongs_to :menu
end
####
The menu item for example of 'Yellow Tail Merlot' belongs to the
section "Wine" under the menu of "Drinks". This is precisely where
I am getting thrown off. I get the feeling that I'm making this
too complicated but when I say it out loud to myself it makes
perfect sense.
好吧,現在這里是帶有habtm的版本2:
class Restaurant
has_many :menus (#Desert, Drinks, Dinner, Lunch, etc.)
end
class Menu
has_and_belongs_to_many :menu_items
end
class MenuItem
has_and_belongs_to_many :menus
has_and_belongs_to_many :sections, class_name: "Menu"
end
如您所見,版本2中的上述代碼比較繁瑣。 實際上,我什至不知道has_and_belongs_to_many的section部分是否可能。 無論如何,這是我從Rails協會文檔中獲取的基本原理。
has_and_belongs_to_many關聯可與另一個模型直接建立多對多連接,而無需中間模型。 例如,如果您的應用程序包含菜單和菜單項,並且每個菜單都有許多菜單項,並且每個菜單項都出現在許多菜單中,則可以用這種方式聲明模型。
引文來自rails docs,但我使用菜單和菜單項切換了裝配件和零件。 以這種方式閱讀完全有道理。 所以現在我問,你們怎么看? 使用哪種更好? 謝謝。
我將使用belongs_to
,而不是在Menu表上使用自連接,而是創建一個名為Section
的新類/表。
更新為:
class Restaurant
has_many :menus
end
class Menu
has_many :menu_items
has_many :sections
belongs_to :restaurant
end
class Section
belongs_to :menu
has_many :menu_items
end
class MenuItem
belongs_to :menu
belongs_to :section
end
您描述的部分屬於更多類別,例如,“酒”是一種酒精飲料,“葡萄酒”是菜單項,“酒精飲料”是類別。 您甚至可以更進一步,將Wine與“酒精”和“飲料”兩個部分相關聯。
我應該使用has_many通過或habtm關聯嗎?
它們都可以用於創建相似的多對多關聯。 關鍵區別在於has_and_belongs_to_many
實體使用模型。 雖然這聽起來很簡單而且很吸引人,但確實有一些弊端:
has_and_belongs_to_many
。 因此has_and_belongs_to_many
非常適合那些零復雜度的情況,在現實生活中不是很好。 對於其他所有內容,都有has_many through:
。
class Restaurant
has_many :menus
has_many :menu_sections, through: :menus
has_many :menu_items, through: :menu_sections
end
class Menu
belongs_to :restaurant
has_many :menu_sections
has_many :menu_items, through: :menu_sections
end
class MenuSection
belongs_to :menu
has_one :restaurant, through: :menu
has_many :menu_items
end
class MenuItem
belongs_to :menu_section
has_one :menu, through: :menu_section
has_one :restaurant, through: :menu
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.