Skip to content

Instantly share code, notes, and snippets.

@marclipovsky
Created March 29, 2012 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marclipovsky/2241307 to your computer and use it in GitHub Desktop.
Save marclipovsky/2241307 to your computer and use it in GitHub Desktop.
scope creating array rather than activerecord::relation
scope :for_account, lambda { |account|
find_by_sql("SELECT 'tickets'.* FROM 'tickets'
INNER JOIN 'groups' ON 'groups'.'id' = 'tickets'.'group_id'
INNER JOIN 'assignments' ON 'groups'.'id' = 'assignments'.'group_id'
INNER JOIN 'accounts' ON 'accounts'.'id' = 'assignments'.'account_id'
WHERE ('accounts'.'id' = #{account.id})
UNION
SELECT 'tickets'.* FROM 'tickets'
INNER JOIN 'tasks' ON 'tasks'.'ticket_id' = 'tickets'.'id'
INNER JOIN 'accounts' ON 'accounts'.'id' = 'tasks'.'account_id'
WHERE ('accounts'.'id' = #{account.id})") }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment