Skip to content

Instantly share code, notes, and snippets.

@kernel-io
Created April 18, 2013 04:30
Show Gist options
  • Save kernel-io/5410139 to your computer and use it in GitHub Desktop.
Save kernel-io/5410139 to your computer and use it in GitHub Desktop.
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