mysql - Rails advanced queries with join and sum calculation -
I have two models: Companies and expense companies have many expenses and expenses are from the company. My expense model has an 'amount' column.
I was wondering if there is a way to spend the searches on a date range and the amount of expenditure column Something like the top 3 companies with the total expenditure in the 7-day period.
I have tried to do this better part of the day to work, I have tried to get involved in it, the nominated scope has to chain, raw SQL etc. And I have no luck.
Thanks for the help. I added it to the model of my company and was able to achieve what I was looking for. ..
named_scope: top_with_expenses ,: joins = & gt; : Expenses, terms = & gt; ['Expenses.created_at & gt; ? ', Time.Now Midnight - 7.days] ,: order = & gt; "Sam (Expenditure) DESC" ,: Group = & gt; "Expenses.company_id",: limit => 3
Comments
Post a Comment