簡體   English   中英

Rails關聯幫助-我是否使用多態關聯?

[英]Rails Association Help - Do I use Polymorphic Association?

我需要幫助來確定對於以下3個模型應該使用哪種關聯。 以下是表格以及我認為當前的關聯。 請讓我知道下面是否有更好的方法。 謝謝

表格的外觀。

User Table
id

Meal Table
id

Comment Table
user_id
Meal_id

我認為這是關聯的外觀,但這可能會根據使用的關聯類型而改變。

User
has_many :comments

Meal
has_many :comments

Comment
belongs_to :user
belongs_to :meal

這聽起來是個好方法。

我可以將belongs_to :user關聯重命名為belongs_to :author, :class_name => "User" ,以便更明確地了解該關聯在您的系統中所扮演的角色。

如果要添加更多模型以進行評論,則應使用多態關聯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM