Rails - Show all models that belongs_to this User

If I have a User that has_many :problems, assuming I have set up the necessary association between User and Problem , how can I do something like:

  # UsersController.rb
  def students_problems
     @userId = params[:user_id]
     @problems = give me all problems associated with this @userId


@user = User.find(params[:user_id])

@problems = @user.problems

Ideally you would want to do this in a model if you plan to do more with your code. So you can create a model method like this for your controller.

@problems = User.name_your_method_here(params[:user_id])

Then in your User model you have


And you might want to add some conditionals to make sure the user_id matches a real user but I'll leave that up to you to do.

EDIT: As lebreeze suggests, it might be wise to change the name of the method to something different, at least to what it relates with what you're doing with your code.

Assuming all your associations and db fields are correct.

@problems = User.find(params[:user_id]).problems

Need Your Help

Connect legacy Haskell code and Fay code

haskell monads fay

I have some Haskell code, and would like a Fay script to be able to access it. The problem is the Haskell code uses monads. Fay doesn't support arbitrary monads. How do I get my Haskell code to wor...