2016-06-29 15 views
0

を持つレコードを返す必要があります。Rails:has_many:through。私のユーザモデルで一意の列値

has_many :followed_recommendations, :through => :followed_users, :source => :recommendations, class_name: Recommendation 

勧告はmovie_id列があります。ユニークなmovie_id値を持つfollowed_recommendationsが、これを実行する方法がないことを確認:だから私は唯一取得する方法が必要になり、ユーザーは同じ映画のために複数の推薦を得ることができますが、私は唯一の1

を返すようにしたいと思います。私が見ている例は、次のとおりです。

has_many :products, -> { distinct }, through: :orders 

しかし、私はちょうど1つの列で区別したいのですが、それは完全な製品レコードを見ています。

答えて

0

あなたは試すました:

has_many :followed_recommendations, ->{group(:movie_id)}, :through => :followed_users, :source => :recommendations, class_name: Recommendation 
+0

私はPG :: GroupingErrorを取得しています:エラー:列 "recommendations.id"は、GROUP BY句に表示するか、集計関数で使用する必要があります –

0

をあなたのRecommendationモデルでmovie_id列を持っているので、私はあなたがこの関連性を持っていると仮定します。

class Recommendation < ActiveRecord::Base 
    belongs_to :movie 
end 

、あなたの質問に投稿されたように、あなたはdistinctと方法を使用することができます。

class User < ActiveRecord::Base 
    has_many :followed_recommendations, :through => :followed_users, :source => :recommendations, class_name: Recommendation 
    has_many :followed_recommended_movies, -> { distinct }, :through => :followed_recommendations, :class_name => Movie, source: :movie 
end 
関連する問題