Skip to content

Instantly share code, notes, and snippets.

@kryzhovnik
Created February 9, 2011 16:19
Show Gist options
  • Save kryzhovnik/818740 to your computer and use it in GitHub Desktop.
Save kryzhovnik/818740 to your computer and use it in GitHub Desktop.
SELECT "users".* FROM "users"
INNER JOIN "addresses" ON "addresses"."user_id" = "users"."id"
INNER JOIN "addresses" "countries_users_join" ON "users"."id" = "countries_users_join"."user_id"
INNER JOIN "countries" ON "countries"."id" = "countries_users_join"."country_id"
INNER JOIN "interests_users" ON "users"."id" = "interests_users"."user_id"
INNER JOIN "interests" ON "interests"."id" = "interests_users"."interest_id"
WHERE
("users"."level" BETWEEN 1 AND 45)
AND ("users"."date_of_birth" BETWEEN '1986-02-09' AND '1991-02-09')
AND ("countries"."title" ILIKE '%d%')
User.find_by_sql(sql).map(&:id) # => [539, 539, 539, 539]
========================================================
SELECT "users".* FROM "users"
INNER JOIN "addresses" ON "addresses"."user_id" = "users"."id"
INNER JOIN "addresses" "countries_users_join" ON "users"."id" = "countries_users_join"."user_id"
INNER JOIN "countries" ON "countries"."id" = "countries_users_join"."country_id"
WHERE
("users"."level" BETWEEN 1 AND 45)
AND ("users"."date_of_birth" BETWEEN '1986-02-09' AND '1991-02-09')
AND ("countries"."title" ILIKE '%d%')
)
User.find_by_sql(sql).map(&:id) # => [500, 539, 584]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment