title: ActiveRecord: complex query with Sum, Join, Group By and Select multiple fields
body: <p>I've waisted few days for this query but still not figured out the solution.</p>
  
  <p><strong>There are my models</strong></p>
  
  <pre><code>class UserObject &lt; ActiveRecord::Base
    self.table_name = "user_objects"
    belongs_to :user, class_name: 'User'
    belongs_to :the_object, class_name: 'Object'
  end
  
  class Object &lt; ActiveRecord::Base
    self.table_name = 'objects'
  
    has_many :user_objects, class_name: 'UserObject', foreign_key: :the_object_id, inverse_of: :the_object
    has_many :users, through: :user_objects
  end
  
  class User &lt; ActiveRecord::Base
    self.table_name = "users"
  end
  </code></pre>
  
  <p><strong>Here is my schema</strong></p>
  
  <pre><code>  create_table "objects", force: true do |t|
      t.float    "importance",        default: 0.5, null: false
    end
  
    create_table "user_objects", force: true do |t|
      t.integer  "user_id"
      t.integer  "the_object_id"
      t.float    "score",              default: 0.0,   null: false
    end
  
    create_table "users", force: true do |t|
      t.string   "first_name"
      t.string   "last_name"
    end
  </code></pre>
  
  <p>I need a query for select <code>objects.importance</code> and sum of <code>user_objects.score</code>. But also I have to select for query only those <code>objects</code> which belongs to <code>user1</code> and <code>user2</code>.</p>
  
  <p>I wrote a query for select objects.importance </p>
  
  <pre><code>Object.select("objects.importance").joins(:user_objects).
                                      where(user_objects: {user_id: [user1_id,user2_id]}).
                                      group(objects: :id).
                                      having('COUNT("user_objects"."id")=2')
  </code></pre>
  
  <p>And it's converted to </p>
  
  <pre><code>SELECT objects.importance FROM "objects" INNER JOIN "user_objects" ON "user_objects"."the_object_id" = "objects"."id" WHERE "user_objects"."user_id" IN (2, 7) GROUP BY "objects"."id" HAVING COUNT("user_objects"."id")=2
  </code></pre>
  
  <p>When I executed this query I got this response</p>
  
  <pre><code>[#&lt;Object id: nil, importance: 0.5&gt;, #&lt;Object id: nil, importance: 0.5&gt;]
  </code></pre>
  
  <p>Quantity of objects in response is OK. But I still don't know how to count in this query sum of <code>user_objects.score</code>. Next query doesn't work</p>
  
  <pre><code>Object.select("objects.importance, SUM(user_objects.score)").
         joins(:user_objects).
         where(user_objects: {user_id: [user1_id,user2_id]}).
         group(objects: :id).having('COUNT("user_objects"."id")=2')
  </code></pre>
  
  <p>I expected in response something like this </p>
  
  <pre><code>[#[&lt;Object id: nil, importance: 0.5&gt;, 0.2], #[&lt;Object id: nil, importance: 0.5&gt;,0.3]]
  </code></pre>
  
  <p>I would greatly appreciate any help you can give me in working this problem.</p>