Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在RubyonRails上为每个类别的每个用户添加评级_Ruby On Rails_Activerecord_Associations_Has Many Through - Fatal编程技术网

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

Ruby on rails 在RubyonRails上为每个类别的每个用户添加评级,ruby-on-rails,activerecord,associations,has-many-through,Ruby On Rails,Activerecord,Associations,Has Many Through,现在我正在构建一个社交媒体应用程序,我想让一个用户对每个类别都有一个评级,协会会怎么做?需要设置的方式是,每个用户在每个类别中都有不同的评级 我认为 belongs_to :user belongs_to :category 在UserCategoryRating模型中 及 在用户模型上,这是正确的方法吗 UserCategoryRating表具有用户id列、类别id列和评级列,每次用户获得投票时,该列都会更新(评级只是投票和基于1-5的分数之间的平均值)我将使用以下数据模型: class U

现在我正在构建一个社交媒体应用程序,我想让一个用户对每个类别都有一个评级,协会会怎么做?需要设置的方式是,每个用户在每个类别中都有不同的评级

我认为

belongs_to :user
belongs_to :category
在UserCategoryRating模型中

在用户模型上,这是正确的方法吗


UserCategoryRating表具有用户id列、类别id列和评级列,每次用户获得投票时,该列都会更新(评级只是投票和基于1-5的分数之间的平均值)

我将使用以下数据模型:

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

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

这将是您的类的基本框架:

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

我只是不确定你为什么想要这个
有很多,:通过
(这看起来不像是多对多--一个评级只属于一个用户,对吗?)

我不认为这就是我想要的,用户不需要有类别,文章需要有类别,我已经做到了。我需要的是每个类别中每个用户的分数/评级。我已经更新了我的答案。我很确定,您将能够使用建议的模型存储用户和类别组合的分数。我正在尝试此解决方案,但它在我的rails控制台上似乎不起作用,我创建了一个UserCategory对象,然后从DB检索了一个类别,后面是一个用户,尝试将它们连接到UC对象,但它不起作用。这个响应也很好!但是另一个很好:)这个问题的标题没有告诉你这个问题是什么——你可能想让它更具描述性。谢谢John,我刚刚更新了我的标题。是的,一个评级只属于一个用户,但是一个用户可以有多个评级(每个类别一个),那就不会有多对多的关系了?即使其他解决方案奏效,你的关系看起来也干净多了,我很想听听你的意见:D@Gotjosh,我不确定我是否完全理解你的问题。。。除了~'score'值之外,一个用户对一个类别的多个评分是否可以区分?我假设它们属于不同的类型,这就是为什么我添加了
:type_of_rating
字段,并添加了一个作用域唯一性验证器。否则,如果评级之间没有真正的区别,您可以删除该行并创建一个验证器,允许用户的评级不需要是唯一的。你明白我想说什么吗?你想要<代码>的原因有很多:通过<代码>是如果你想<代码>用户<代码>和<代码>类别<代码>彼此拥有某种所有权--在这种情况下,他们没有。虽然它们都与
Rating
s有一个1-*关系,但是通过
Rating
s“数据库中这些模型的两个外键和一些漂亮的Rails”方法完全维护了这种关系。现在,您可以想去
:通过
评分
模型,但您的要求似乎也没有必要这样做(如果您想做
User.find(1).categories
——获得用户投票的所有类别)。我将更新我的答案,如果您还有问题,请告诉我。PS:你试过这个吗?你得到你所期望的了吗?
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)