Skip to content

Instantly share code, notes, and snippets.

@stevenharman
Created January 21, 2013 20:44
Show Gist options
  • Save stevenharman/4589186 to your computer and use it in GitHub Desktop.
Save stevenharman/4589186 to your computer and use it in GitHub Desktop.
Using Arel/ActiveRecord to execute a subquery, including a SUM.
select fund_sums.total, fund_products.name
from (
select sum(allocation_amount) total, fund_product_id
from transactions
where transactions.investor_id = 490
group by fund_product_id
) fund_sums
left join fund_products on fund_sums.fund_product_id = fund_products.id
@martinstreicher
Copy link

@stevenharman Thanks for this gist. It helped a lot. What is the magic of this:

manager = Arel::SelectManager.new(tt.engine)

Why .engine?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment