Rails - Show all models that belongs_to this User -


if have user has_many :problems, assuming have set necessary association between user , problem , how can like:

  # userscontroller.rb   def students_problems      @userid = params[:user_id]      @problems = give me problems associated @userid   end 

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

ideally want in model if plan more code. can create model method controller.

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

then in user model have

self.name_your_method_here(user_id)   user.find(user_id).problems end 

and might want add conditionals make sure user_id matches real user i'll leave do.

edit: lebreeze suggests, might wise change name of method different, @ least relates you're doing code.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -