Reputation: 25
I'm starting to work with ActiveRecord outside Rails to handle a legacy MySQL database that I cannot alter. I want to group rows of a table by date, but AR takes the whole datetime field and not just the date part. What I've tried so far is:
raw_data = Order.group(:date_add).sum(:total_products).to_a
Any ideas?
Thanks
Upvotes: 1
Views: 41
Reputation: 37617
You can group
by a SQL expression instead of just a column, so write a SQL expression that returns the datetime's date and group
by that:
raw_data = Order.group("date(date_add)").sum(:total_products).to_a
Upvotes: 1