0

I have a model of:

User has_many recipes, recipe belongs_to user

recipe has_many comments, comment belongs_to recipe

My Recipes is nested to comments and I can access comments on Recipes_controller without problem.

If I try to access it from user, I got undefined method error.

My users_controller:

@user = User.find(params[:id])
@recipes = @user.recipes.paginate(page: param[:page])
@comments = @user.recipes.comments

How do i access comments from user?

doc_ufo
  • 77
  • 2
  • 7

1 Answers1

0

@user.recipes is an array of all the recipes for that user. You have to pick a specific one to call "comments" on. Something like

@user.recipes.first.comments
Scott S
  • 2,696
  • 16
  • 20
  • inside my comments, i have a rating, I would like to average the comments for all the recipes that the user have. Is there a way to do this? I'm using <%= @recipe.rcomments.average(:rating) %> inside the recipes – doc_ufo Nov 28 '12 at 15:41
  • Have a look at the answer for this question - http://stackoverflow.com/questions/1341271/average-from-a-ruby-array. You will need to extrapolate from this, since you are actually averaging values in a nested array, and you are trying to average "comment.rating" instead of just integers, but that should give you a good start. – Scott S Nov 28 '12 at 15:59