Created
July 6, 2012 04:53
-
-
Save subimage/3058191 to your computer and use it in GitHub Desktop.
Gnarly Rails scope of the day
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
# Projects a user has access to either through ProjectAssignment, or as an "account manager" | |
named_scope :user_has_access_to, lambda{|user| | |
{ | |
:select => 'DISTINCT projects.*', | |
:joins => %q\ | |
LEFT JOIN project_assignments | |
ON ( | |
projects.id = project_assignments.project_id | |
AND project_assignments.has_access = 1 | |
) | |
LEFT JOIN account_manager_memberships | |
ON ( | |
projects.client_id = account_manager_memberships.client_id | |
AND projects.client_type = account_manager_memberships.client_type | |
) | |
\, | |
:conditions => [ | |
%q\ | |
(project_assignments.person_id = ?) | |
OR (account_manager_memberships.person_id = ?) | |
\, | |
user.id, user.id | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment