Posted By

insanedreamer on 08/04/10


Tagged

activerecord


Versions (?)

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


 / Published in: Rails
 

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

  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
Posted By: insanedreamer on August 4, 2010

Will return something like:

IRELAND johnny patricia yolanda Katie Michelle ITALY jerry

You need to login to post a comment.