Groupdate2

groupdate2 is an enhanced version of the beautiful Groupdate gem, it adds more features to Groupdate

  • SQL Server 2016+ support
  • series_label support

Installation

Add this line to your application’s Gemfile:

gem 'groupdate2'

Features

SQL Server 2016+

MS SQL Server (2016 and above) support by using (AT TIME ZONE).

series_label, an option to include group clause in selected result

User case

If you use groupdate not with ActiveRecord::Calculations, but manually selecting the calculation like:

sql = "COUNT(*) AS count, AVG(delivery.volume/truck.capacity) AS percentage, MIN(collected_at) AS collected_at"

Delivery.select(sql).group_by_day(:collected_at).to_a

It works well except the series label is not included in the result. I have to use MIN(collected_at) to have it, then convert it to the correct label in ruby code. It works but is cumbersome.

With this new option series_label: collected_at_date, it indicates the group clause should be included in the result and can be accessed by the method collected_at_date.

Notes

  • !!! This option does NOT work with ActiveRecord::Calculations.
  • The series_label respects other Groupdate options like :locale, :dates and :format

See more options

An example

users = User.select('COUNT(*) AS total, AVG(age) as average_age').group_by_month(:created_at, series_label: :created_at_month)

#The returned result would have attributes:
users.first.created_at_month
users.first.total
users.first.average_age

Upgrading

5.0.x

  • SQL server support
  • series_label support

4.1.x

This version corresponds to groupdate 4.1.2 with SQL Server support.