Rails advanced queries with join and sum calculation
- by Dustin Brewer
I have two models: companies and expenses. Companies have many expenses and expenses belong to companies. My expense model has an 'amount' column.
I was wondering if there is a way to perform a find based on a date range and the amount column of the expenses. Something like top 3 companies by total expense amounts over a 7 day period.
I've tried for the better part of the day to get this to work, I've attempted joins, chaining named scopes, raw sql, etc. and I'm not having any luck.
Thanks for the help.