Rails 用户评价其他用户
我正在开发一个应用程序,用户可以在其中向另一个用户留下反馈。目前,我已经实现了用户模型。
这是否被视为自我参照关联?
这对我来说似乎有点奇怪(如何通过活动记录关联进行设置)。
我将如何建立这个协会,它是什么类型的协会?在进行此操作时我需要注意什么(提示、建议、可能出现的错误)?
根据下面的答案这是这样吗?
(用户和评分之间一对多,评分和用户之间一对一)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能不是 Rails/Active Record 术语,而是 ORM 通用术语。
我可能会通过拥有反馈或评论模型来实现这一点,并且您的用户模型将具有一对多的反馈/评论,并且反馈/评论将与创建它的用户具有一对一的关系。
编辑:对问题更新的响应...
您可以在作为作者的评级模型上拥有一个用户对象。我的 Rails 经验很少,但我相信您的评级模型看起来像这样。
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.