title: ActiveRecord select add count
body: <p>In my ActiveRecord query, I need to provide this info in the select method:</p>
  
  <pre><code>(SELECT count(*) from likes where likes.spentit_id = spentits.id) as like_count,
  (SELECT count(*) from comments where comments.spentit_id = spentits.id) as comment_count
  </code></pre>
  
  <p>Of course, I pass pass these two as string to the .select() part, but I am wondering what's the proper/alternative way of doing this?</p>
  
  <p>Here's the complete query I am trying to call:</p>
  
  <pre><code>SELECT DISTINCT
      spentits.*,
      username,
      (SELECT count(*) from likes where likes.spentit_id = spentits.id) as like_count,
      (SELECT count(*) from comments where comments.spentit_id = spentits.id) as comment_count,
      (SELECT count(*) from wishlist_items where wishlist_items.spentit_id = spentits.id) as wishlist_count,
      (case when likes.id is null then 0 else 1 end) as is_liked_by_me,
      (case when wishlist_items.id is null then 0 else 1 end) as is_wishlisted_by_me,
      (case when comments.id is null then 0 else 1 end) as is_commented_by_me
  FROM spentits
  LEFT JOIN users ON users.id = spentits.user_id
  LEFT JOIN likes ON likes.user_id = 9 AND likes.spentit_id = spentits.id
  LEFT JOIN wishlist_items ON wishlist_items.user_id = 9 AND wishlist_items.spentit_id = spentits.id
  LEFT JOIN comments ON comments.user_id = 9 AND comments.spentit_id = spentits.id
  WHERE spentits.user_id IN
      (SELECT follows.following_id
       FROM follows
       WHERE follows.follower_id = 9 AND follows.accepted = 1)
  ORDER BY id DESC LIMIT 15 OFFSET 0;
  </code></pre>
  
  <p>All the tables here have their respective ActiveRecord object. Just really confused how to convert this query into 'activerecord'/rails way with writing least amount of SQL. The '9' user_id is suppose to be a parameter.</p>
  
  <p><strong>Update:</strong> 
  Ok so here's what I did inmean time, it's much better than raw SQL statement, but it still looks ugly to me:</p>
  
  <pre><code>class Spentit &lt; ActiveRecord::Base
    belongs_to :user
    has_many :likes
    has_many :wishlist_items
    has_many :comments
  
    scope :include_author_info, lambda {
      joins([:user]).
      select("username").
      select("users.photo_uri as user_photo_uri").
      select("spentits.*")
    }
  
    scope :include_counts, lambda {
      select("(SELECT count(*) from likes where likes.spentit_id = spentits.id) as like_count").
      select("(SELECT count(*) from comments where comments.spentit_id = spentits.id) as comment_count").
      select("(SELECT count(*) from wishlist_items where wishlist_items.spentit_id = spentits.id) as wishlist_items_count").
      select("spentits.*")
    }
  end
  </code></pre>
  
  <p>Using these scope methods, I can do:</p>
  
  <pre><code>Spentit.where(:id =&gt; 7520).include_counts.include_author_info.customize_for_user(45)
  </code></pre>
  
  <p>A bit about the classes. A <code>User</code> has many <code>Spentits</code>. A <code>Spentit</code> has many <code>comments</code>, <code>likes</code> and <code>comments</code>. </p>