Created
October 9, 2011 03:17
-
-
Save codespore/1273225 to your computer and use it in GitHub Desktop.
Left outer joining 2 fields referring to same User model in Rails 3 (Part 2)
This file contains hidden or 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
| t = Task.arel_table | |
| u = User.arel_table | |
| c = u.alias("creators_tasks") | |
| keyword = "%#{params}%" | |
| relation = includes(:user_assigned,:creator) | |
| .where( | |
| t[:id].matches(keyword) | |
| .or(u[:username].matches(keyword)) | |
| .or(c[:username].matches(keyword)) | |
| ) | |
| # Outputs | |
| # SELECT * FROM tasks LEFT OUTER JOIN users ON users.id = tasks.user_assigned_id AND users.deleted_at IS NULL LEFT OUTER | |
| # JOIN users creators_tasks ON creators_tasks.id = tasks.creator_id AND creators_tasks.deleted_at IS NULL WHERE | |
| # users.username LIKE 'Manager%' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment