Skip to content

Instantly share code, notes, and snippets.

@shunchu
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shunchu/112903dce62572342549 to your computer and use it in GitHub Desktop.
Save shunchu/112903dce62572342549 to your computer and use it in GitHub Desktop.
add associated object's IDs as arrays for each record in collection
# add associated object's IDs as arrays for each record in collection
Organization.joins(:users)
.select("organizations.*, '#{User.joins(:organization).pluck(:id)}' as user_ids")
.group('organizations.id')
# => { organizations: [{ id: 1, name: 'some name',... user_ids:[1,2,3] }] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment