Skip to content

Instantly share code, notes, and snippets.

@Pomeha
Created May 8, 2018 09:21
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 Pomeha/505e88d8070d7688ef72a1558f4ad541 to your computer and use it in GitHub Desktop.
Save Pomeha/505e88d8070d7688ef72a1558f4ad541 to your computer and use it in GitHub Desktop.
ApplicationRecord.connection.execute('SELECT clients.*,
(CASE WHEN agents_clients.id is NULL then 0 else 1 END)
AS joined FROM clients
LEFT JOIN agents_clients
on clients.id = agents_clients.client_id
INNER JOIN users ON users.id = clients.user_id
WHERE agents_clients.id is NULL OR agents_clients.agent_id = 4
ORDER BY joined, users.name'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment