title: How to connect ransacker query to ransack sort search parameter
body: <p><strong>Problem:</strong></p>
  
  <p>I am using the ransack gem to sort columns in a table. I have 2 models: Campaign and Course. A campaign has many courses, and a course belongs to one campaign. Each course has a number of total_attendees. My Campaigns table has a column for Total Attendees, and I want it to be sortable. So it would sum up the total_attendees field for each course that belongs to a single campaign, and sort based on that sum.</p>
  
  <p>Ex. A campaign has 3 courses, each with 10 attendees. The Total Attendees column on the campaign table would show 30 and it would be sortable against total attendees for all the other campaigns.</p>
  
  <p>I found ransackers:
  <a href="https://github.com/activerecord-hackery/ransack/wiki/Using-Ransackers" rel="nofollow noreferrer">https://github.com/activerecord-hackery/ransack/wiki/Using-Ransackers</a></p>
  
  <p>and this SO question: <a href="https://stackoverflow.com/questions/47399360/ransack-sort-by-sum-of-relation">Ransack sort by sum of relation</a> </p>
  
  <p>and from that put together a lot of what is below.</p>
  
  <p><strong>From Model - campaign.rb:</strong></p>
  
  <pre><code>class Campaign &lt; ApplicationRecord
    has_many :courses
  
    ransacker :sum_of_total_attendees do
      query = "SELECT SUM(r.total_attendees)
                FROM campaigns c
     LEFT OUTER JOIN courses r
                  ON r.campaign_id = c.id
            GROUP BY c.id"
      Arel.sql(query)
    end
  end
  </code></pre>
  
  <p><strong>From Model - course.rb:</strong></p>
  
  <pre><code>class Course &lt; ApplicationRecord
    belongs_to :campaign, optional: true
  end
  </code></pre>
  
  <p><strong>View:</strong></p>
  
  <pre><code>&lt;th scope="col"&gt;&lt;%= sort_link(@q, :sum_of_total_attendees, 'Total Attendees') %&gt;&lt;/th&gt;
  </code></pre>
  
  <p><strong>Controller - campaigns_controller.rb:</strong></p>
  
  <pre><code>all_campaigns = Campaign.all
  @q = all_campaigns.ransack(params[:q])
  @campaigns = @q.result
  </code></pre>
  
  <p><strong>Errors:</strong></p>
  
  <p>The ransacker query gives me the data I want, but I don't know what to do to get the right information .  </p>
  
  <p>Originally, when I clicked on the <code>th</code> link to sort the data, I got this error:</p>
  
  <pre><code>PG::CardinalityViolation: ERROR: more than one row returned by a 
  subquery used as an expression
  </code></pre>
  
  <p>I don't know what changed, but now I'm getting this error:</p>
  
  <pre><code>PG::SyntaxError: ERROR:  syntax error at or near "SELECT"
  LINE 1: SELECT "campaigns".* FROM "campaigns" ORDER BY SELECT SUM(r....
                                                     ^
  : SELECT "campaigns".* FROM "campaigns" ORDER BY SELECT 
  SUM(r.total_attendees)
            FROM campaigns c
      LEFT OUTER JOIN courses r
              ON r.campaign_id = c.id
        GROUP BY c.id ASC
  </code></pre>
  
  <p>This error seems to say that the ransack search parameter, @q and the ransacker query don't work together.  There are two selects in this request, when there should definitely be only one, but the first one is coming from ransack, so I'm not sure how to address it.</p>
  
  <p>How do I get my query to sort correctly with ransack?</p>
  
  <p>Articles I've looked at but did not seem to apply to what I was looking to accomplish with this story:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/47399360/ransack-sort-by-sum-of-relation">Ransack Sort By Sum of Relation</a>:  This is the one I worked from a lot, but I'm not sure why it works for this user and not for me.  They don't show what is changed, if anything, in the controller</li>
  <li><a href="https://github.com/activerecord-hackery/ransack/issues/277#issuecomment-26628593" rel="nofollow noreferrer">Ransack Github Issue For Multiple Params</a>: This doesn't cover the issue of summing table columns.</li>
  <li><a href="https://stackoverflow.com/questions/15261071/rails-ransack-sorting-searching-based-on-a-definition-in-the-model">Rails Ransack Sorting Searching Based On A Definition In The Model</a>: This didn't apply to my need to sort based on summed data.</li>
  <li><a href="https://www.projectidealism.com/posts/2015/1/12/three-ways-you-can-bend-the-ransack-gem-to-your-liking" rel="nofollow noreferrer">Three Ways to Bend The Ransack Gem</a>: This looks like what I was doing, but I'm not sure why theirs is working but mine isn't.</li>
  </ul>