Skip to content

Instantly share code, notes, and snippets.

@qcam
Created May 23, 2014 05:40
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 qcam/a5a55210b67cd40793b9 to your computer and use it in GitHub Desktop.
Save qcam/a5a55210b67cd40793b9 to your computer and use it in GitHub Desktop.
Rails Active Record Query

Rails Active Record Query

INNER JOIN

Category.joins(:posts)

# SELECT categories.* FROM categories INNER JOIN posts ON posts.category_id = categories.id

LEFT OUTER JOIN

Client.includes("orders").where(first_name: 'Ryan', orders: { status: 'received' })

# SELECT count(DISTINCT clients.id) AS count_all FROM clients
# LEFT OUTER JOIN orders ON orders.client_id = client.id WHERE
# (clients.first_name = 'Ryan' AND orders.status = 'received')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment