Rails を使用して JSON 応答をクライアントに送信しています。また、関連付けの節と一緒にオプションrespond_with
を使用する方法を見つけようとしています。includes
respond_with
where
ここに私のモデルがあります:
class User < ActiveRecord::Base
has_many :ratings
has_many :movies, through: :ratings
end
class Rating < ActiveRecord::Base
belongs_to :user
belongs_to :movie
end
class Movie < ActiveRecord::Base
has_many :ratings
has_many :users, through: :ratings
end
私のコントローラーアクションには、次のものがあります。
def create
movies = Movie.order("RANDOM()").limit(3)
respond_with(movies, include: :ratings)
// I'd like to do something like
// respond_with(movies, include: :ratings WHERE user: current_user)
end
ただし、これはこれら 3 つの映画のすべての評価で応答しています。特定のユーザーだけの評価に制限したい