title: Rails order by association
body: <p>My goal is to sort the posts by Total amount of votes. As you can see in the controller, at first I sorted the posts by created_at, which worked! However I wanted to change it so it would be sorted by total votes. The votes come from the acts_as_votable gem. Which is associated with Post</p>
  
  <p>Currently this is my posts controller: </p>
  
  <pre><code>    def index
          # @posts = Post.all.order("created_at DESC") &lt;--This works
          @posts = Post.includes(:votes).order("votes_for.size desc")
      end
  </code></pre>
  
  <p>When I run this I get the following error:Association named 'votes' was not found on Post; perhaps you misspelled it?</p>
  
  <p>My guess is that includes does not work here.</p>
  
  <p>When I open up rails console. To test the association I get the following:</p>
  
  <pre><code>@post = Post.first
  @post.votes_for.size
  </code></pre>
  
  <p>It shows that I currently have 2 votes.</p>
  
  <p>Lastly here is my Post.rb Model</p>
  
  <pre><code>class Post &lt; ActiveRecord::Base
      acts_as_votable
      belongs_to :user
      has_many :comments
      has_attached_file :image, styles: { medium: "700x500&gt;#", small: "350x250&gt;" }
      validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
  end
  </code></pre>
  
  <p>Thanks in advance!!!
  (I am using psql)</p>
  
  <p>Add Schema:</p>
  
  <pre><code> create_table "posts", force: :cascade do |t|
      t.string   "title"
      t.string   "link"
      t.text     "description"
      t.datetime "created_at",         null: false
      t.datetime "updated_at",         null: false
      t.integer  "user_id"
      t.string   "image_file_name"
      t.string   "image_content_type"
      t.integer  "image_file_size"
      t.datetime "image_updated_at"
    end
  
    create_table "users", force: :cascade do |t|
      t.string   "email",                  default: "", null: false
      t.string   "encrypted_password",     default: "", null: false
      t.string   "reset_password_token"
      t.datetime "reset_password_sent_at"
      t.datetime "remember_created_at"
      t.integer  "sign_in_count",          default: 0,  null: false
      t.datetime "current_sign_in_at"
      t.datetime "last_sign_in_at"
      t.string   "current_sign_in_ip"
      t.string   "last_sign_in_ip"
      t.datetime "created_at",                          null: false
      t.datetime "updated_at",                          null: false
      t.string   "name"
    end
  
    add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
    add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
  
    create_table "votes", force: :cascade do |t|
      t.integer  "votable_id"
      t.string   "votable_type"
      t.integer  "voter_id"
      t.string   "voter_type"
      t.boolean  "vote_flag"
      t.string   "vote_scope"
      t.integer  "vote_weight"
      t.datetime "created_at"
      t.datetime "updated_at"
    end
  
    add_index "votes", ["votable_id", "votable_type", "vote_scope"], name: "index_votes_on_votable_id_and_votable_type_and_vote_scope", using: :btree
    add_index "votes", ["voter_id", "voter_type", "vote_scope"], name: "index_votes_on_voter_id_and_voter_type_and_vote_scope", using: :btree
  
    add_foreign_key "comments", "posts"
    add_foreign_key "comments", "users"
  end
  </code></pre>