Rails:困难的SQL查询has_many:通过

时间:2016-07-05 22:34:35

标签: ruby-on-rails activerecord

这是我的用户模型:

has_many :follower_connections, :class_name => "Following", :foreign_key => :user_id
has_many :followed_users, :through => :following_connections
has_many :followed_recommendations, 
           :through => :followed_users, 
           :source => :recommendations, 
           class_name: Recommendation

建议:

has_many :ratings
has_many :recommended_trailers, :through => :movie, class_name: Trailer, source: :trailers

scope :unrated, -> (id) { 
    includes(:ratings).where("ratings.user_id != ?", id).references(:ratings)
  }

评分:

class Rating < ActiveRecord::Base
  belongs_to :user
  belongs_to :recommendation
  validates :user_id, :uniqueness => {:scope => [:recommendation_id]}
end

基本上作为用户,您可以关注其他用户。您可以向其他用户推荐电影(推荐)并查看其他用户&#39;推荐电影。

每个用户还可以评价其他用户&#39;建议。

我想要做的是加载所有给定的用户&#39; follow_recommendations 尚未被该用户评级(一个推荐可以被评为我的许多用户)。

我打电话:

current_user.followed_recommendations.unrated(current_user.id)

但是它返回了一些由current_user评价的推荐。

关于我出错的地方的任何想法?

0 个答案:

没有答案
相关问题