Skip to content

Instantly share code, notes, and snippets.

@AlexVPopov
Created September 8, 2016 12:42
Show Gist options
  • Save AlexVPopov/aa1a21176d15a11de0f2a2913a49fff8 to your computer and use it in GitHub Desktop.
Save AlexVPopov/aa1a21176d15a11de0f2a2913a49fff8 to your computer and use it in GitHub Desktop.
# User.joins(accounts: :broker_server)
SELECT "users".*
FROM "users"
INNER JOIN "broker_accounts"
ON "broker_accounts"."user_id" = "users"."id"
AND "broker_accounts"."state" IN ('new', 'active', 'inactive', 'disabled', 'out-of-money')
INNER JOIN "broker_servers"
ON "broker_servers"."id" = "broker_accounts"."broker_server_id"
WHERE "users"."active" = 't'
# User.joins(real_accounts: :broker_server)
SELECT "users".*
FROM "users"
INNER JOIN "broker_accounts"
ON "broker_accounts"."user_id" = "users"."id"
AND "broker_accounts"."state" IN ('new', 'active', 'inactive', 'disabled', 'out-of-money')
AND (broker_accounts.fake_type = 'real' OR broker_servers.type = 'real')
INNER JOIN "broker_servers" ON "broker_servers"."id" = "broker_accounts"."broker_server_id"
WHERE "users"."active" = 't'
# User.joins(accounts: :broker_server).joins(real_accounts: :broker_server)
SELECT "users".*
FROM "users"
INNER JOIN "broker_accounts"
ON "broker_accounts"."user_id" = "users"."id"
AND "broker_accounts"."state" IN ('new', 'active', 'inactive', 'disabled', 'out-of-money')
INNER JOIN "broker_servers"
ON "broker_servers"."id" = "broker_accounts"."broker_server_id"
INNER JOIN "broker_accounts" "real_accounts_users" ON "real_accounts_users"."user_id" = "users"."id"
AND "real_accounts_users"."state" IN ('new', 'active', 'inactive', 'disabled', 'out-of-money')
AND (broker_accounts.fake_type = 'real' OR broker_servers.type = 'real')
INNER JOIN "broker_servers" "broker_servers_broker_accounts"
ON "broker_servers_broker_accounts"."id" = "real_accounts_users"."broker_server_id"
WHERE "users"."active" = 't'
# User.joins(accounts: :broker_server).joins(:real_accounts)
SELECT "users".*
FROM "users"
INNER JOIN "broker_accounts"
ON "broker_accounts"."user_id" = "users"."id"
AND "broker_accounts"."state" IN ('new', 'active', 'inactive', 'disabled', 'out-of-money')
INNER JOIN "broker_servers"
ON "broker_servers"."id" = "broker_accounts"."broker_server_id"
INNER JOIN "broker_accounts" "real_accounts_users"
ON "real_accounts_users"."user_id" = "users"."id"
AND "real_accounts_users"."state" IN ('new', 'active', 'inactive', 'disabled', 'out-of-money')
AND (broker_accounts.fake_type = 'real' OR broker_servers.type = 'real')
WHERE "users"."active" = 't'
SELECT DISTINCT users.*
FROM users
INNER JOIN broker_accounts
ON broker_accounts.user_id = users.id
AND broker_accounts.state IN ('new', 'active', 'inactive', 'disabled', 'out-of-money')
INNER JOIN broker_servers
ON broker_servers.id = broker_accounts.broker_server_id
INNER JOIN broker_accounts real_accounts_users
ON real_accounts_users.user_id = users.id
AND real_accounts_users.state IN ('new', 'active', 'inactive', 'disabled', 'out-of-money')
AND (broker_accounts.fake_type = 'real' OR broker_servers.type = 'real')
WHERE users.active = 't'
SELECT DISTINCT users.*
FROM users
INNER JOIN broker_accounts
ON broker_accounts.user_id = users.id
AND broker_accounts.state IN ('new', 'active', 'inactive', 'disabled', 'out-of-money')
AND broker_accounts.fake_type = 'real'
INNER JOIN broker_servers
ON broker_servers.id = broker_accounts.broker_server_id
AND broker_server.type = 'real'
WHERE users.active = 't'
SELECT DISTINCT users.* FROM users INNER JOIN broker_accounts ON broker_accounts.user_id = users.id AND broker_accounts.state IN ('new', 'active', 'inactive', 'disabled', 'out-of-money') AND broker_accounts.fake_type = 'real'INNER JOIN broker_servers ON broker_servers.id = broker_accounts.broker_server_id AND broker_server.type = 'real'WHERE users.active = 't';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment