Skip to content

Instantly share code, notes, and snippets.

@abhishek0
Created August 14, 2013 07:42
Show Gist options
  • Save abhishek0/6228784 to your computer and use it in GitHub Desktop.
Save abhishek0/6228784 to your computer and use it in GitHub Desktop.
users = Arel::Table.new(:users)
stamps = Arel::Table.new(:stamps)
contacts = Arel::Table.new(:contacts)
children = users.alias
query = users.where(users[:user_type].eq('franchisee'))
query = query.join(stamps).on(users[:id].eq(stamps[:stampable_id]))
query = query.join(contacts).on(contacts[:id].eq(stamps[:contact_id]))
query = query.join(children).on(users[:id].eq(children[:parent_id]))
query = query.join(stamps).on(children[:id].eq(stamps[:stampable_id]))
query = query.group users[:id], stamps[:status]
query = query.project(users[:id], stamps[:status].count.as("count"), stamps[:status], contacts[:first_name], contacts[:last_name])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment