Group Rails query into arrays by attribute Group Rails query into arrays by attribute sql sql

Group Rails query into arrays by attribute


Got it!

People.all.group_by(&:age)


If you're actually dealing with JSON:

people.group_by{|p| p['age'] }

If you're dealing with ActiveRecord models:

People.group('id, age')

Here there's additional documentation on grouping with ActiveRecord.