returning an array of objects grouped by a field (in sub groups)


/ Published in: Rails
Save to your folder(s)

Allows iteration through the main array, with subarrays with the records for each group.


Copy this code and paste it in your HTML
  1. User.order(:country_id).group_by(&:country_id).each { |country| country.each { |usergroup| if usergroup.is_a?(Array) then usergroup.each { |user| puts user.name } else puts Country.find(usergroup).name end }}

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.