我使用的軌道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'列。該查詢將引發異常。 –