title: Rails Custom SQL Query not working with find_by_sql
body: <p>I have three Tables users, user_groups and user_groups_users in my Rails Application. Now, I have written an SQL query</p>
  
  <pre><code>SELECT user_group_id FROM user_groups_users GROUP BY user_group_id HAVING SUM(CASE WHEN user_id IN (1,4) THEN 1 ELSE 0 END) = COUNT(*)
  </code></pre>
  
  <p>to extract those user_group_id, which exactly has the user_ids provided by the input. In this case 1,4 would be provided as an ruby array [1,4].</p>
  
  <p>Now, i want to write this SQL query for my rails application. But when I execute this query using <code>find_by_sql</code> statement in rails,it is returning me the correct no. of user_groups in which the particular user_ids belong, but the result does not show the user_group_id.</p>
  
  <p><code>=&gt; [#&lt;UserGroup &gt;, #&lt;UserGroup &gt;, #&lt;UserGroup &gt;, #&lt;UserGroup &gt;, #&lt;UserGroup &gt;, #&lt;UserGroup &gt;]</code></p>
  
  <p>so how can i extract User_group_id from it.</p>
  
  <p>However, when I execute the query by connecting directly to the database. I am getting the desired results</p>
  
  <p><code>sql = "SELECT user_group_id FROM user_groups_users WHERE user_id IN (1,2) GROUP BY user_group_id HAVING Count(DISTINCT user_id) = 2;"</code></p>
  
  <p><code>records_array = ActiveRecord::Base.connection.execute(sql)</code></p>
  
  <p>But, here the problem is, how would I use the String replacement technique, to use the user_ids given as input.</p>