Rails 7.1 adds regroup
to ActiveRecord::Relation. regroup
overrides the
existing group condition with a new one. regroup
is a
short-hand for unscope(:old_group_fields).group(:new_group_fields)
Here is how it can be used.
Project.group(:title, :owner)
# SELECT "projects".* FROM "projects" GROUP BY "projects"."title", "projects"."owner"
Project.group(:title, :owner).regroup(:priority)
# SELECT "projects".* FROM "projects" GROUP BY "projects"."priority"
Here is the relevant pull request adding this change.