我使用的轨道respond_with
发送JSON响应到客户端关联,而我试图找出如何连同where
子句中我联想轨道4 respond_with包括与WHERE
includes
选项
respond_with
这里是我的模型:
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
如何有史以来,这是三个电影的所有收视率作出回应。我想限制它只是特定用户的
你可以在movies变量上添加一个where语句 - 就像'movies = Movie.where(user_id:@ user.id).order ...' – abbott567
@ abbott567,那是行不通的。在提问者的模式中'Movie'模型没有'user_id'列。该查询将引发异常。 –