Last active
December 21, 2015 01:19
-
-
Save abhishek0/6226892 to your computer and use it in GitHub Desktop.
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
User.includes(stamp: [:contact]).where(:user_type => 'franchisee').joins(:stamp).joins(:children).group("users.id, stamps.status").count("stamps.status") |
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
SELECT COUNT(DISTINCT stamps.status) AS count_stamps_status, users.id, stamps.status AS users_id_stamps_status | |
FROM "users" INNER JOIN "stamps" ON "stamps"."stampable_id" = "users"."id" AND "stamps"."stampable_type" = 'User' | |
INNER JOIN "users" "children_users" ON "children_users"."parent_id" = "users"."id" | |
LEFT OUTER JOIN "contacts" ON "contacts"."id" = "stamps"."contact_id" | |
WHERE "users"."user_type" = 'franchisee' GROUP BY users.id, stamps.status |
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
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]) |
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
SELECT "users"."id", COUNT("stamps"."status") AS count, "stamps"."status", "contacts"."first_name", "contacts"."last_name" | |
FROM "users" INNER JOIN "stamps" ON "users"."id" = "stamps"."stampable_id" | |
INNER JOIN "contacts" ON "contacts"."id" = "stamps"."contact_id" | |
INNER JOIN "users" "users_2" ON "users"."id" = "users_2"."parent_id" | |
INNER JOIN "stamps" ON "users_2"."id" = "stamps"."stampable_id" | |
WHERE "users"."user_type" = 'franchisee' | |
GROUP BY "users"."id", "stamps"."status" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment