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
Post a Comment