在 Ruby on Rails 上为每个类别的每个用户添加评级
现在我正在构建一个社交媒体应用程序,我希望用户对每个类别进行评分,该关联将如何进行?需要设置的方式是每个用户在每个类别中都会有不同的评级。
我认为
belongs_to :user
belongs_to :category
在 UserCategoryRating 模型中。
在
has_many :user_category_ratings, through => :category
用户模型上,这是正确的方法吗?
UserCategoryRating 表包含 User_id 列、Category_id 列和评级列,每次用户获得投票时都会更新(评级只是投票之间的 AVG 和基于 1-5 的分数)
Right now I'm building a social media app, where i want an user to have a rating per category, how would the association go? The way it needs to be setup it's Each user will have a different rating in each category.
I'm think that
belongs_to :user
belongs_to :category
in the UserCategoryRating model.
and
has_many :user_category_ratings, through => :category
on the User model, Is this the correct approach?
The UserCategoryRating table has the User_id column, Category_id column, and the rating column, that updates each time an user gets votes (The rating it's just the AVG between votes and the score based on 1-5)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新:如果我理解正确,这里是您想要的简单设计的图表:
这将是基本框架你的课程:
将允许这些查询:
我只是不确定你为什么想要这个has_many, :through
(这看起来不像是多对多——只是一个评级属于一个用户,对吗?)。UPDATE: If I'm understanding you correctly, here is a diagram of the simple design you'd like:
And this would be the basic skeleton of your classes:
Will allow for these query:
I'm just unsure as to why you want thishas_many, :through
(this doesn't seem like a many to many -- a rating only belongs to one user, correct?).我将使用以下数据模型:
现在,当您想要更新某个类别的用户分数时
I will use the following data model:
Now when you want to update the score of a user for a category