Q:

Rails model scope and group

I would like to get all users that have a registration stage of 1, 2, 3, 4, 5, or 6 and then group them in registration stages.
class User < ActiveRecord::Base

    scope :incomplete_onboarding, -> { where(registration_stage: [1, 2, 3, 4, 5]).group('registration_stage') }

end

A:

Maybe the ruby's group_by is a better option for what you want to accomplish, so you can remove the group from the scope
class User < ActiveRecord::Base

    scope :incomplete_onboarding, -> { where(registration_stage: [1, 2, 3, 4, 5]) }

end

And then when you call it you can group it:
User.incomplete_onboarding.group_by { |u| u.registration_stage }

This will result in an hash like this
{ 1 => [user1, user2], 2 => [user3] } 

