Rails advanced queries with join and sum calculation

Posted by Dustin Brewer on Stack Overflow See other posts from Stack Overflow or by Dustin Brewer
Published on 2010-06-13T23:46:15Z Indexed on 2010/06/13 23:52 UTC
Read the original article Hit count: 254

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about ruby-on-rails