Sometime you have relationship like campaign has_many causes and need to calculate sum of raise_amount raised from each campaign from the associated table
In mysql this can be done by a simple query
select campaigns.name,SUM(raises.raise_amount) as total_sum FROM campaigns LEFT OUTER JOIN raises ON campaigns.id=raises.campaign_id GROUP BY campaigns.id;
In Rails it can be achieved by this query
Campaign.joins(“LEFT OUTER JOIN raises on campaigns.id = raises.campaign_id”).group(“campaigns.id”).order(“SUM(raises.raise_amount) DESC”)
it takes a couple of hours to figure it out. might it save others time.