Reputation: 25134
I have two tables relevant to this question.
The first is Company
which has_many
Taps
. Each Tap has a Date field that states the date when it was created (different than created_at
for reasons irrelevant here).
I want to plot the frequency of Taps
for a company over time, grouping by day. That means for my 30-day graph, I am currently making a separate SQL query for each Date
in my range to get app taps where :tapped_time = {some date} and :company_id = {id of the company}
.
How can I get the number of Taps for a Company on each day in a consecutive range without making a separate query for each day?
I am using Ruby on Rails, so I don't need raw SQL I just need some help with my ActiveRecord magic.
Upvotes: 1
Views: 545
Reputation: 95562
In SQL, you'd group by date if you're querying for a single company, by date and company if you're querying for multiple companies at one time.
ActiveRecord has a group method for that.
Upvotes: 2