title: acts_as_votable ordering based on upvotes in controller
body: <p>I am using the acts_as_votable gem to order my links based on how many up-votes they get from users. </p>
  
  <p>I used a .each iterator in my view originally to handle the ordering and it worked fine:</p>
  
  <p>@links.order(cached_votes_up: :desc).each do |link|</p>
  
  <p>But now I am trying to include the ordering functionality in my controller and it is not working. I tried a few different ways:</p>
  
  <pre><code>class LinksController &lt; ApplicationController
    before_filter :authenticate_user!, except: [:index, :show]
  
    def index
      @links = Link.order(cached_votes_up: :desc)
      @links = Link.paginate(page: params[:page], per_page: 5)
  
      respond_to do |format| 
        format.js
        format.html
      end
    end
  end
  
      class LinksController &lt; ApplicationController
        before_filter :authenticate_user!, except: [:index, :show]
  
        def index
          @links = Link.order(cached_votes_up: :desc).paginate(page: params[:page], per_page: 5)
  
          respond_to do |format| 
            format.js
            format.html
          end
        end
  end
  
  
  class LinksController &lt; ApplicationController
    before_filter :authenticate_user!, except: [:index, :show]
  
    def index
      @links = Link.order(cached_votes_up: :desc)
      @links.paginate(page: params[:page], per_page: 5)
  
      respond_to do |format| 
        format.js
        format.html
      end
    end
  end
  </code></pre>
  
  <p>My _link partial:</p>
  
  <pre><code>&lt;div class="link row clearfix"&gt;
      &lt;h2&gt;
        &lt;%= image_tag link.avatar.thumb %&gt;
      &lt;/h2&gt;
      &lt;h2&gt;
        &lt;%= link_to link.title, link %&gt;&lt;br&gt;
      &lt;/h2&gt;
      &lt;p&gt;
        &lt;%= link_to link.url, link %&gt;&lt;br&gt;
      &lt;/p&gt;
  
  
  &lt;!-- acts_as_votable for like_link --&gt;
      &lt;div class="btn-group"&gt;
          &lt;%= link_to like_link_path(link), method: :put, class: "btn btn-default btn-sm" do %&gt;
            &lt;span class="glyphicon glyphicon-chevron-up"&gt;&lt;/span&gt;
            Upvote
            &lt;%= link.get_upvotes.size %&gt;
          &lt;% end %&gt;
          &lt;%= link_to dislike_link_path(link), method: :put, class: "btn btn-default btn-sm" do %&gt;
            &lt;span class="glyphicon glyphicon-chevron-down"&gt;
            Downvote
            &lt;%= link.get_downvotes.size %&gt;
          &lt;% end %&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>index.html.erb:</p>
  
  <pre><code>&lt;div id="link"&gt;&lt;%= render @links %&gt;&lt;/div&gt;
  
  &lt;%= will_paginate @links %&gt;
  </code></pre>
  
  <p>Does anyone see what the problem could be or what I may be doing wrong?</p>
  
  <p>This is the output when I run <code>Link.order(cached_votes_up: :desc)</code>. Even though I downvoted this post, it is still returning the object as containing an upvote?</p>
  
  <pre><code>irb(main):001:0&gt; @link = Link.order(cached_votes_up: :desc).first
    Link Load (2.0ms)  SELECT  "links".* FROM "links"  ORDER BY "links"."cached_votes_up" DESC LIMIT 1
  =&gt; #&lt;Link id: 16, title: "John F Kennedy", url: "Change is the law of life. And those who look only...", created_at: "2015-12-26 23:56:26", updated_at: "2015-12-26 23:56:31", user_id: nil, cached_votes_total: 1, cached_votes_score: 1, cached_votes_up: 1, cached_votes_down: 0, avatar: "jfk.jpeg"&gt;
  </code></pre>
  
  <p>This is my link model containing the acts_as_votable object:</p>
  
  <pre><code>class Link &lt; ActiveRecord::Base
      self.per_page = 5
      belongs_to :user
      validates :user, presence: true
      acts_as_votable
      attr_accessor :avatar
      mount_uploader :avatar, AvatarUploader
  end
  </code></pre>
  
  <p>This is my routes file:</p>
  
  <pre><code>Rails.application.routes.draw do
    devise_for :users
    resources :users
    resources :links do
      member do
        put "like", to: "links#upvote"
        put "dislike", to: "links#downvote"
      end
    end
    root to: "links#index"
  </code></pre>
  
  <p>These are my downvote and upvote actions:</p>
  
  <pre><code>  def upvote
      @link = Link.find(params[:id])
      @link.upvote_by current_user
      redirect_to :back
    end
  
    def downvote
      @link = Link.find(params[:id])
      @link.downvote_from current_user
      redirect_to :back
    end
  </code></pre>