と の間に必要な関連付けを設定したと仮定して、User
thatがある場合、次のようなことを行うにはどうすればよいですか。has_many :problems
User
Problem
# UsersController.rb
def students_problems
@userId = params[:user_id]
@problems = give me all problems associated with this @userId
end
と の間に必要な関連付けを設定したと仮定して、User
thatがある場合、次のようなことを行うにはどうすればよいですか。has_many :problems
User
Problem
# UsersController.rb
def students_problems
@userId = params[:user_id]
@problems = give me all problems associated with this @userId
end
すべての関連付けと db フィールドが正しいと仮定します。
@problems = User.find(params[:user_id]).problems
@user = User.find(params[:user_id])
@problems = @user.problems
コードでさらに多くのことを行う予定がある場合は、モデルでこれを行うのが理想的です。したがって、コントローラー用にこのようなモデル メソッドを作成できます。
@problems = User.name_your_method_here(params[:user_id])
次に、 User モデルに
self.name_your_method_here(user_id)
User.find(user_id).problems
end
また、実際のユーザーと一致することを確認するためにいくつかの条件を追加することもできuser_id
ますが、それはあなたに任せます。
編集: lebreeze が示唆するように、メソッドの名前を別の名前に変更するのが賢明かもしれません。