Rails 用户评价其他用户

发布于 2024-08-28 09:28:39 字数 380 浏览 3 评论 0原文

我正在开发一个应用程序,用户可以在其中向另一个用户留下反馈。目前,我已经实现了用户模型。

这是否被视为自我参照关联?

这对我来说似乎有点奇怪(如何通过活动记录关联进行设置)。

我将如何建立这个协会,它是什么类型的协会?在进行此操作时我需要注意什么(提示、建议、可能出现的错误)?

根据下面的答案这是这样吗?

(用户和评分之间一对多,评分和用户之间一对一)

class User < ActiveRecord::Base
    has_many :ratings
end

class Ratings < ActiveRecord::Base
    belongs_to :user
end

I am working on an application where a user has the ability to leave feedback on another user. Currently, I already have an implemented user model.

Is this considered a self referential association?

This seems a bit wierd to me (how to set it up through Active Record Associations).

How would I go about setting this association up, what type of association is it? Anything I need to watch out for while going about this (tips, suggestions, errors that may come up)?

According to Answers Below Would This be the Way?

(one to many between users and ratings and one to one between ratings and user)

class User < ActiveRecord::Base
    has_many :ratings
end

class Ratings < ActiveRecord::Base
    belongs_to :user
end

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

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

发布评论

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

评论(1

唐婉 2024-09-04 09:28:39

这可能不是 Rails/Active Record 术语,而是 ORM 通用术语。

我可能会通过拥有反馈或评论模型来实现这一点,并且您的用户模型将具有一对多的反馈/评论,并且反馈/评论将与创建它的用户具有一对一的关系。

编辑:对问题更新的响应...

您可以在作为作者的评级模型上拥有一个用户对象。我的 Rails 经验很少,但我相信您的评级模型看起来像这样。

class Ratings < ActiveRecord::Base
    belongs_to :user
    has_one :user
end

This may not be in Rails/Active Record terms but ORM generic.

I would probably implement this by having a Feedback or Review model and your User model would have one-to-many Feedback/Reviews and the Feedback/Review would have a one-to-one relationship with the User who created it.

EDIT: Response to question update...

You can have an User object on the Ratings model that is the author. My rails experience is minimal but I believe your Ratings model would look like this.

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