Skip to content

Instantly share code, notes, and snippets.

@osiro
Created December 5, 2014 08:16
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 osiro/7389fad23f77222677e6 to your computer and use it in GitHub Desktop.
Save osiro/7389fad23f77222677e6 to your computer and use it in GitHub Desktop.
heaps_of_users_wishlists = UsersWishlist.where(user: user)
heaps_of_groups = BookIdentifierGroup.where(id: heaps_of_users_wishlists.pluck(:book_identifier_group_id)
the_book_identifiers_I_wanna = BookIdentifier.where(group_id: heaps_of_groups.pluck(:id))
puts the_book_identifiers_I_wanna
# This shit triggers 3 queries
heaps_of_users_wishlists = UsersWishlist.where(user: user)
heaps_of_groups = BookIdentifierGroup.joins(:users_wishlists).merge(heaps_of_users_wishlists)
the_book_identifiers_I_wanna = BookIdentifier.joins(:group).merge(heaps_of_groups)
# And this shit triggers 1 query
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment