Rails Association 帮助 - 我是否使用多态关联?

发布于 2024-12-21 19:54:23 字数 333 浏览 5 评论 0原文

我需要帮助来确定应该对以下 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

I need help determining what kind of associations I should use for the following 3 models. Below are the tables and what I think the current association is. Please let me know if there is a better way to achieve below. Thanks

How the tables should look.

User Table
id

Meal Table
id

Comment Table
user_id
Meal_id

Here is what I think the associations look, but this may change depending on the type of association is used.

User
has_many :comments

Meal
has_many :comments

Comment
belongs_to :user
belongs_to :meal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剪不断理还乱 2024-12-28 19:54:23

这听起来是个好方法。

我会将 belongs_to :user 关联重命名为 belongs_to :author, :class_name => “用户” 更明确地说明关联在您的系统中扮演的角色。

如果您想添加更多模型进行评论,您应该使用多态关联

That sound like a good way to do it.

I would rename the belongs_to :user association to belongs_to :author, :class_name => "User" to be more explicit about the role the association plays in your system.

If you want to add more models to comment on, you should use polymorphic associations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文