在 Ruby on Rails 上为每个类别的每个用户添加评级

发布于 2024-09-10 06:34:18 字数 404 浏览 9 评论 0原文

现在我正在构建一个社交媒体应用程序,我希望用户对每个类别进行评分,该关联将如何进行?需要设置的方式是每个用户在每个类别中都会有不同的评级。

我认为

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

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

发布评论

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

评论(2

忆梦 2024-09-17 06:34:18

更新:如果我理解正确,这里是您想要的简单设计的图表:

UML 图

这将是基本框架你的课程:

class User < ActiveRecord::Base
   has_many :ratings
   # has_many :categories, :through => :ratings
end

class Category < ActiveRecord::Base
   has_many :ratings
   # has_many :users, :through => :ratings
end

class Rating < ActiveRecord::Base
    belongs_to :user
    belongs_to :category
    validates_uniqueness_of :user_id, :scope => [:category_id]
end

将允许这些查询:

@category_ratings_by_user = Rating.where("ratings.user_id = ? AND ratings.category_id = ?", user_id, category_id)
@specific_rating = user.ratings.where("ratings.category_id = ?", category_id)
# make nice model methods, you know the deal

# ... if you added the has_many :through,
@john = User.find_by_name("john")
# Two ways to collect all categories that john's ratings belong to:
@johns_categories_1 = @john.ratings.collect { |rating| rating.category }
@johns_categories_2 = @john.categories

@categories_john_likes = @john.categories.where("categories.rating >= ?", 7)

我只是不确定你为什么想要这个has_many, :through(这看起来不像是多对多——只是一个评级属于一个用户,对吗?)。

UPDATE: If I'm understanding you correctly, here is a diagram of the simple design you'd like:

UML Diagram

And this would be the basic skeleton of your classes:

class User < ActiveRecord::Base
   has_many :ratings
   # has_many :categories, :through => :ratings
end

class Category < ActiveRecord::Base
   has_many :ratings
   # has_many :users, :through => :ratings
end

class Rating < ActiveRecord::Base
    belongs_to :user
    belongs_to :category
    validates_uniqueness_of :user_id, :scope => [:category_id]
end

Will allow for these query:

@category_ratings_by_user = Rating.where("ratings.user_id = ? AND ratings.category_id = ?", user_id, category_id)
@specific_rating = user.ratings.where("ratings.category_id = ?", category_id)
# make nice model methods, you know the deal

# ... if you added the has_many :through,
@john = User.find_by_name("john")
# Two ways to collect all categories that john's ratings belong to:
@johns_categories_1 = @john.ratings.collect { |rating| rating.category }
@johns_categories_2 = @john.categories

@categories_john_likes = @john.categories.where("categories.rating >= ?", 7)

I'm just unsure as to why you want this has_many, :through (this doesn't seem like a many to many -- a rating only belongs to one user, correct?).

夏见 2024-09-17 06:34:18

我将使用以下数据模型:

class User
  has_many :user_categories
  has_many :categories, :through => :user_categories
end

class UserCategory
  belongs_to :user
  belongs_to :category
  # this model stores the average score also.
end

class Category
  has_many :user_categories
  has_many :users, :through => :user_categories
end

现在,当您想要更新某个类别的用户分数时

uc = u.user_categories.find_by_category_id(id)
uc.score = score
uc.save

I will use the following data model:

class User
  has_many :user_categories
  has_many :categories, :through => :user_categories
end

class UserCategory
  belongs_to :user
  belongs_to :category
  # this model stores the average score also.
end

class Category
  has_many :user_categories
  has_many :users, :through => :user_categories
end

Now when you want to update the score of a user for a category

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