Search code examples
sqlruby-on-railsvoting-system

Rails. How to get stat column from votes table where user_id = current user


I have petition site. I building now the voting system, but thats don't work glad. To create only one vote per user I use this condition in view:

<% if @post.votes.where(user_id: current_user.id).blank? %> 

It's return true if user do not vote for current post.

But when i want to show user vote statement with this code:

<% if @post.votes(user_id: current_user.id) == 1 %>
        "u voted LIKE"
       <% else %>
        "u voted DISLIKE"
       <% end %>

it return me the error: We're sorry, but something went wrong (500) I'm in development mode. Thanks.


Solution

  • You are missing where and count in the statement.

    try this,

     <% if @post.votes.where(user_id: current_user.id).count.eql?(1) %>
        "u voted LIKE"
     <% else %>
        "u voted DISLIKE"
     <% end %>