Displaying count for current_user 'likes' on a 'question' in questions/show.html.erb view

Posted by bgadoci on Stack Overflow See other posts from Stack Overflow or by bgadoci
Published on 2010-05-20T20:38:28Z Indexed on 2010/05/20 20:40 UTC
Read the original article Hit count: 147

I have an application that allows for users to create questions, create answers to questions, and 'like' answers of others. When a current_user lands on the /views/questions/show.html.erb page I am trying to display the total 'likes' for all answers on that question, for the current_user.

In my Likes table I am collecting the question_id, site_id and the user_id and I have the appropriate associations set up between user, question, answers and likes. I think I just need to call this information the right way, which I can't seem to figure out. All of the below is taking place in the /views/questions/show.html.erb page.

I have tried the following:

    <% div_for current_user do %>
        You have liked this question <%= @question.likes.count %> times
    <% end %>

Which returns all the 'likes' for the question but not filtered by current_user

You have liked this question <%= current_user.question.likes.count %> times

Which gives an error of 'undefined method `question'

You have liked this question <%= @question.current_user.likes.count %> times

Which gives an error of 'undefined method `current_user'

I have tried a couple of other things but they don't make as much sense as the above to me. What am I missing?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby