Skip to content

Instantly share code, notes, and snippets.

@slaskis
Created November 5, 2010 01:18
Show Gist options
  • Save slaskis/663508 to your computer and use it in GitHub Desktop.
Save slaskis/663508 to your computer and use it in GitHub Desktop.
module DataMapper
class Collection
# A query friendlier group_by method
def group_by
assoc = {}
self.each do |item|
key = yield(item)
if assoc.has_key? key
assoc[key] << item
else
q = {}
item.class.key.each_with_index {|k,i| q[k.name] = item.key[i] }
assoc[key] = all(q)
end
end
assoc
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment