Rails 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
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这听起来是个好方法。
我会将
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 tobelongs_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.