sebasimone
sebasimone

Reputation: 25

Grouping a datetime column by date in a legacy database using ActiveRecord

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

Answers (1)

Dave Schweisguth
Dave Schweisguth

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

Related Questions