Created
April 18, 2013 04:30
-
-
Save kernel-io/5410139 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
def for_user_with_role_sql(id, name) | |
sql = <<-SQL | |
WITH RECURSIVE organization_tree AS | |
( SELECT o.* | |
FROM organizations AS o | |
JOIN roles AS r | |
ON r.manageable_id = o.id | |
AND r.user_id = #{id} | |
AND r.manageable_type = 'Organization' | |
AND r.name = '#{name}' | |
UNION ALL | |
SELECT o.* | |
FROM organizations AS o | |
JOIN organization_tree | |
ON ( | |
o.parent_id = organization_tree.id | |
) | |
) | |
SELECT * FROM organization_tree | |
SQL | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment