-
-
Save xab3r/7356fbf78d748247fde8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simple JOIN | |
User.joins(:account) # User -> Account | |
# Will produce | |
# SELECT `users`.* FROM `users` INNER JOIN `accounts` ON `accounts`.`user_id` = `users`.`id` | |
# Complicated JOIN | |
Trait.joins(:user => :account) # Trait -> User -> Account | |
# Will produce | |
# SELECT `traits`.* FROM `traits` INNER JOIN `users` ON `users`.`id` = `traits`.`user_id` INNER JOIN `accounts` ON `accounts`.`user_id` = `users`.`id` | |
# And more complicated JOIN | |
Completion.joins(:trait => {:user => :account}) # Completion -> Trait -> User -> Account | |
# Will produce | |
# SELECT `completions`.* FROM `completions` INNER JOIN `traits` ON `traits`.`id` = `completions`.`trait_id` INNER JOIN `users` ON `users`.`id` = `traits`.`user_id` INNER JOIN `accounts` ON `accounts`.`user_id` = `users`.`id` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment