{"action":"created","comment":{"url":"https://api.github.com/repos/ManageIQ/manageiq/pulls/comments/153388570","pull_request_review_id":79382730,"id":153388570,"diff_hunk":"@@ -93,17 +93,33 @@ def define_virtual_aggregate_method(name, relation, method_name, column)\n     def virtual_aggregate_arel(reflection, method_name, column)\n       return unless reflection && reflection.macro == :has_many && !reflection.options[:through]\n       lambda do |t|\n+        query = if reflection.scope\n+                  reflection.klass.instance_exec(nil, &reflection.scope)\n+                else\n+                  reflection.klass.all\n+                end\n+\n+        # ordering will probably screw up aggregations, so clear this out from\n+        # any calls\n+        #\n+        # only clear this out if this isn't a `:size` call as well, since doing\n+        # a COUNT(*) will allow any ORDER BY to still work properly.  This is\n+        # to avoid any possible edge cases by clearing out the order clause.\n+        query.order_values = [] if method_name != :size\n+\n         foreign_table = reflection.klass.arel_table\n         # need db access for the keys, so delaying all this lookup until call time\n-        local_key = reflection.active_record_primary_key\n+        local_key   = reflection.active_record_primary_key\n         foreign_key = reflection.foreign_key\n+        query       = query.where(t[local_key].eq(foreign_table[foreign_key]))\n+\n         arel_column = if method_name == :size\n                         Arel.star.count\n                       else\n                         reflection.klass.arel_attribute(column).send(method_name)\n                       end\n-        t.grouping(foreign_table.project(arel_column)\n-                                .where(t[local_key].eq(foreign_table[foreign_key])))\n+\n+        t.grouping(Arel::Nodes::SqlLiteral.new(query.select(arel_column).to_sql))","path":"lib/extensions/virtual_total.rb","position":33,"original_position":33,"commit_id":"bd7eaa5255b75dbb5d222d6a4ea0aaf2446304fe","original_commit_id":"bd7eaa5255b75dbb5d222d6a4ea0aaf2446304fe","user":{"login":"kbrock","id":1930,"avatar_url":"https://avatars2.githubusercontent.com/u/1930?v=4","gravatar_id":"","url":"https://api.github.com/users/kbrock","html_url":"https://github.com/kbrock","followers_url":"https://api.github.com/users/kbrock/followers","following_url":"https://api.github.com/users/kbrock/following{/other_user}","gists_url":"https://api.github.com/users/kbrock/gists{/gist_id}","starred_url":"https://api.github.com/users/kbrock/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kbrock/subscriptions","organizations_url":"https://api.github.com/users/kbrock/orgs","repos_url":"https://api.github.com/users/kbrock/repos","events_url":"https://api.github.com/users/kbrock/events{/privacy}","received_events_url":"https://api.github.com/users/kbrock/received_events","type":"User","site_admin":false},"body":"Ugh. I am frustrated that `Arel` makes you do this. 😦 \r\nI do recognize the pattern and 👍 😦 ","created_at":"2017-11-28T03:37:59Z","updated_at":"2017-11-28T03:50:52Z","html_url":"https://github.com/ManageIQ/manageiq/pull/16518#discussion_r153388570","pull_request_url":"https://api.github.com/repos/ManageIQ/manageiq/pulls/16518","author_association":"MEMBER","_links":{"self":{"href":"https://api.github.com/repos/ManageIQ/manageiq/pulls/comments/153388570"},"html":{"href":"https://github.com/ManageIQ/manageiq/pull/16518#discussion_r153388570"},"pull_request":{"href":"https://api.github.com/repos/ManageIQ/manageiq/pulls/16518"}}},"pull_request":{"url":"https://api.github.com/repos/ManageIQ/manageiq/pulls/16518","id":153975995,"html_url":"https://github.com/ManageIQ/manageiq/pull/16518","diff_url":"https://github.com/ManageIQ/manageiq/pull/16518.diff","patch_url":"https://github.com/ManageIQ/manageiq/pull/16518.patch","issue_url":"https://api.github.com/repos/ManageIQ/manageiq/issues/16518","number":16518,"state":"open","locked":false,"title":"Fixes virtual_totals when a default_scope exists","user":{"login":"NickLaMuro","id":314014,"avatar_url":"https://avatars0.githubusercontent.com/u/314014?v=4","gravatar_id":"","url":"https://api.github.com/users/NickLaMuro","html_url":"https://github.com/NickLaMuro","followers_url":"https://api.github.com/users/NickLaMuro/followers","following_url":"https://api.github.com/users/NickLaMuro/following{/other_user}","gists_url":"https://api.github.com/users/NickLaMuro/gists{/gist_id}","starred_url":"https://api.github.com/users/NickLaMuro/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NickLaMuro/subscriptions","organizations_url":"https://api.github.com/users/NickLaMuro/orgs","repos_url":"https://api.github.com/users/NickLaMuro/repos","events_url":"https://api.github.com/users/NickLaMuro/events{/privacy}","received_events_url":"https://api.github.com/users/NickLaMuro/received_events","type":"User","site_admin":false},"body":"This is an alternative solution from the one provided in https://github.com/ManageIQ/manageiq/pull/16512 .  Borrowed the tests from that PR though (thanks @lpichler !)\n\nWhen a `has_many` relation of a model that is used in a `virtual_total` and has a default scope, it was not getting applied to the arel query when the `virtual_total` column was being used in the `.select`.  An example in `CloudTenant`:\n\n```ruby\nclass CloudTenant < ApplicationRecord\n\n  has_many   :vms, -> { active }\n\n  virtual_total :total_vms, :vms\n\n  # ...\nend\n```\n\nThen with flowing `ActiveRecord` query:\n\n```ruby\nCloudTenant.where(:name => 'DEV').select(:total_vms)\n```\n\nWould produce:\n\n```sql\nSELECT (\n  (SELECT COUNT(*)\n   FROM \"vms\"\n   WHERE \"cloud_tenants\".\"id\" = \"vms\".\"cloud_tenant_id\")\n  ) AS total_vms\nFROM \"cloud_tenants\"\nWHERE \"cloud_tenants\".\"name\" = 'DEV'\n```\n\nWhich doesn't take into account the `active` scope, which will filter out any VM without an `ems_id` (so templates).\n\nWith this change, the SQL executed then gets updated to the following:\n\n```sql\nSELECT (\n  (SELECT COUNT(*)\n   FROM \"vms\"\n   WHERE \"cloud_tenants\".\"id\" = \"vms\".\"cloud_tenant_id\"\n     AND \"vms\".\"type\" IN ('Vm', 'ManageIQ::Providers::CloudManager::Vm',\n                          'ManageIQ::Providers::InfraManager::Vm', 'VmServer',\n                          'ManageIQ::Providers::Amazon::CloudManager::Vm',\n                          'ManageIQ::Providers::Azure::CloudManager::Vm',\n                          'ManageIQ::Providers::Google::CloudManager::Vm',\n                          'ManageIQ::Providers::Openstack::CloudManager::Vm',\n                          'ManageIQ::Providers::Vmware::CloudManager::Vm', 'VmXen',\n                          'ManageIQ::Providers::Redhat::InfraManager::Vm',\n                          'ManageIQ::Providers::Microsoft::InfraManager::Vm',\n                          'ManageIQ::Providers::Vmware::InfraManager::Vm')\n     AND (\"vms\".\"ems_id\" IS NOT NULL))\n  ) AS total_vms\nFROM \"cloud_tenants\"\nWHERE \"cloud_tenants\".\"name\" = 'DEV'\n```\n\nWhich properly includes the default scopes for both the `Vm` model, and the `:vm` relation in `CloudTenant`.\n\n\nLinks\n-----\n* Alternative to https://github.com/ManageIQ/manageiq/pull/16512\n* https://bugzilla.redhat.com/show_bug.cgi?id=1494589","created_at":"2017-11-21T19:07:54Z","updated_at":"2017-11-28T03:50:52Z","closed_at":null,"merged_at":null,"merge_commit_sha":"eedbb587b509b871a156163d3740216175731f21","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/ManageIQ/manageiq/pulls/16518/commits","review_comments_url":"https://api.github.com/repos/ManageIQ/manageiq/pulls/16518/comments","review_comment_url":"https://api.github.com/repos/ManageIQ/manageiq/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ManageIQ/manageiq/issues/16518/comments","statuses_url":"https://api.github.com/repos/ManageIQ/manageiq/statuses/bd7eaa5255b75dbb5d222d6a4ea0aaf2446304fe","head":{"label":"NickLaMuro:virtual_total_fix_with_existing_scope_on_relation","ref":"virtual_total_fix_with_existing_scope_on_relation","sha":"bd7eaa5255b75dbb5d222d6a4ea0aaf2446304fe","user":{"login":"NickLaMuro","id":314014,"avatar_url":"https://avatars0.githubusercontent.com/u/314014?v=4","gravatar_id":"","url":"https://api.github.com/users/NickLaMuro","html_url":"https://github.com/NickLaMuro","followers_url":"https://api.github.com/users/NickLaMuro/followers","following_url":"https://api.github.com/users/NickLaMuro/following{/other_user}","gists_url":"https://api.github.com/users/NickLaMuro/gists{/gist_id}","starred_url":"https://api.github.com/users/NickLaMuro/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NickLaMuro/subscriptions","organizations_url":"https://api.github.com/users/NickLaMuro/orgs","repos_url":"https://api.github.com/users/NickLaMuro/repos","events_url":"https://api.github.com/users/NickLaMuro/events{/privacy}","received_events_url":"https://api.github.com/users/NickLaMuro/received_events","type":"User","site_admin":false},"repo":{"id":60729101,"name":"manageiq","full_name":"NickLaMuro/manageiq","owner":{"login":"NickLaMuro","id":314014,"avatar_url":"https://avatars0.githubusercontent.com/u/314014?v=4","gravatar_id":"","url":"https://api.github.com/users/NickLaMuro","html_url":"https://github.com/NickLaMuro","followers_url":"https://api.github.com/users/NickLaMuro/followers","following_url":"https://api.github.com/users/NickLaMuro/following{/other_user}","gists_url":"https://api.github.com/users/NickLaMuro/gists{/gist_id}","starred_url":"https://api.github.com/users/NickLaMuro/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NickLaMuro/subscriptions","organizations_url":"https://api.github.com/users/NickLaMuro/orgs","repos_url":"https://api.github.com/users/NickLaMuro/repos","events_url":"https://api.github.com/users/NickLaMuro/events{/privacy}","received_events_url":"https://api.github.com/users/NickLaMuro/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/NickLaMuro/manageiq","description":"ManageIQ Cloud Management Platform","fork":true,"url":"https://api.github.com/repos/NickLaMuro/manageiq","forks_url":"https://api.github.com/repos/NickLaMuro/manageiq/forks","keys_url":"https://api.github.com/repos/NickLaMuro/manageiq/keys{/key_id}","collaborators_url":"https://api.github.com/repos/NickLaMuro/manageiq/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/NickLaMuro/manageiq/teams","hooks_url":"https://api.github.com/repos/NickLaMuro/manageiq/hooks","issue_events_url":"https://api.github.com/repos/NickLaMuro/manageiq/issues/events{/number}","events_url":"https://api.github.com/repos/NickLaMuro/manageiq/events","assignees_url":"https://api.github.com/repos/NickLaMuro/manageiq/assignees{/user}","branches_url":"https://api.github.com/repos/NickLaMuro/manageiq/branches{/branch}","tags_url":"https://api.github.com/repos/NickLaMuro/manageiq/tags","blobs_url":"https://api.github.com/repos/NickLaMuro/manageiq/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/NickLaMuro/manageiq/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/NickLaMuro/manageiq/git/refs{/sha}","trees_url":"https://api.github.com/repos/NickLaMuro/manageiq/git/trees{/sha}","statuses_url":"https://api.github.com/repos/NickLaMuro/manageiq/statuses/{sha}","languages_url":"https://api.github.com/repos/NickLaMuro/manageiq/languages","stargazers_url":"https://api.github.com/repos/NickLaMuro/manageiq/stargazers","contributors_url":"https://api.github.com/repos/NickLaMuro/manageiq/contributors","subscribers_url":"https://api.github.com/repos/NickLaMuro/manageiq/subscribers","subscription_url":"https://api.github.com/repos/NickLaMuro/manageiq/subscription","commits_url":"https://api.github.com/repos/NickLaMuro/manageiq/commits{/sha}","git_commits_url":"https://api.github.com/repos/NickLaMuro/manageiq/git/commits{/sha}","comments_url":"https://api.github.com/repos/NickLaMuro/manageiq/comments{/number}","issue_comment_url":"https://api.github.com/repos/NickLaMuro/manageiq/issues/comments{/number}","contents_url":"https://api.github.com/repos/NickLaMuro/manageiq/contents/{+path}","compare_url":"https://api.github.com/repos/NickLaMuro/manageiq/compare/{base}...{head}","merges_url":"https://api.github.com/repos/NickLaMuro/manageiq/merges","archive_url":"https://api.github.com/repos/NickLaMuro/manageiq/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/NickLaMuro/manageiq/downloads","issues_url":"https://api.github.com/repos/NickLaMuro/manageiq/issues{/number}","pulls_url":"https://api.github.com/repos/NickLaMuro/manageiq/pulls{/number}","milestones_url":"https://api.github.com/repos/NickLaMuro/manageiq/milestones{/number}","notifications_url":"https://api.github.com/repos/NickLaMuro/manageiq/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/NickLaMuro/manageiq/labels{/name}","releases_url":"https://api.github.com/repos/NickLaMuro/manageiq/releases{/id}","deployments_url":"https://api.github.com/repos/NickLaMuro/manageiq/deployments","created_at":"2016-06-08T20:36:58Z","updated_at":"2016-06-08T20:37:19Z","pushed_at":"2017-11-28T01:24:57Z","git_url":"git://github.com/NickLaMuro/manageiq.git","ssh_url":"git@github.com:NickLaMuro/manageiq.git","clone_url":"https://github.com/NickLaMuro/manageiq.git","svn_url":"https://github.com/NickLaMuro/manageiq","homepage":"manageiq.org","size":209471,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"ManageIQ:master","ref":"master","sha":"1c5f5e493650965667bf9d9d126145c789f86012","user":{"login":"ManageIQ","id":1125575,"avatar_url":"https://avatars0.githubusercontent.com/u/1125575?v=4","gravatar_id":"","url":"https://api.github.com/users/ManageIQ","html_url":"https://github.com/ManageIQ","followers_url":"https://api.github.com/users/ManageIQ/followers","following_url":"https://api.github.com/users/ManageIQ/following{/other_user}","gists_url":"https://api.github.com/users/ManageIQ/gists{/gist_id}","starred_url":"https://api.github.com/users/ManageIQ/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ManageIQ/subscriptions","organizations_url":"https://api.github.com/users/ManageIQ/orgs","repos_url":"https://api.github.com/users/ManageIQ/repos","events_url":"https://api.github.com/users/ManageIQ/events{/privacy}","received_events_url":"https://api.github.com/users/ManageIQ/received_events","type":"Organization","site_admin":false},"repo":{"id":20789757,"name":"manageiq","full_name":"ManageIQ/manageiq","owner":{"login":"ManageIQ","id":1125575,"avatar_url":"https://avatars0.githubusercontent.com/u/1125575?v=4","gravatar_id":"","url":"https://api.github.com/users/ManageIQ","html_url":"https://github.com/ManageIQ","followers_url":"https://api.github.com/users/ManageIQ/followers","following_url":"https://api.github.com/users/ManageIQ/following{/other_user}","gists_url":"https://api.github.com/users/ManageIQ/gists{/gist_id}","starred_url":"https://api.github.com/users/ManageIQ/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ManageIQ/subscriptions","organizations_url":"https://api.github.com/users/ManageIQ/orgs","repos_url":"https://api.github.com/users/ManageIQ/repos","events_url":"https://api.github.com/users/ManageIQ/events{/privacy}","received_events_url":"https://api.github.com/users/ManageIQ/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ManageIQ/manageiq","description":"ManageIQ Open-Source Management Platform","fork":false,"url":"https://api.github.com/repos/ManageIQ/manageiq","forks_url":"https://api.github.com/repos/ManageIQ/manageiq/forks","keys_url":"https://api.github.com/repos/ManageIQ/manageiq/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ManageIQ/manageiq/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ManageIQ/manageiq/teams","hooks_url":"https://api.github.com/repos/ManageIQ/manageiq/hooks","issue_events_url":"https://api.github.com/repos/ManageIQ/manageiq/issues/events{/number}","events_url":"https://api.github.com/repos/ManageIQ/manageiq/events","assignees_url":"https://api.github.com/repos/ManageIQ/manageiq/assignees{/user}","branches_url":"https://api.github.com/repos/ManageIQ/manageiq/branches{/branch}","tags_url":"https://api.github.com/repos/ManageIQ/manageiq/tags","blobs_url":"https://api.github.com/repos/ManageIQ/manageiq/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ManageIQ/manageiq/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ManageIQ/manageiq/git/refs{/sha}","trees_url":"https://api.github.com/repos/ManageIQ/manageiq/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ManageIQ/manageiq/statuses/{sha}","languages_url":"https://api.github.com/repos/ManageIQ/manageiq/languages","stargazers_url":"https://api.github.com/repos/ManageIQ/manageiq/stargazers","contributors_url":"https://api.github.com/repos/ManageIQ/manageiq/contributors","subscribers_url":"https://api.github.com/repos/ManageIQ/manageiq/subscribers","subscription_url":"https://api.github.com/repos/ManageIQ/manageiq/subscription","commits_url":"https://api.github.com/repos/ManageIQ/manageiq/commits{/sha}","git_commits_url":"https://api.github.com/repos/ManageIQ/manageiq/git/commits{/sha}","comments_url":"https://api.github.com/repos/ManageIQ/manageiq/comments{/number}","issue_comment_url":"https://api.github.com/repos/ManageIQ/manageiq/issues/comments{/number}","contents_url":"https://api.github.com/repos/ManageIQ/manageiq/contents/{+path}","compare_url":"https://api.github.com/repos/ManageIQ/manageiq/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ManageIQ/manageiq/merges","archive_url":"https://api.github.com/repos/ManageIQ/manageiq/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ManageIQ/manageiq/downloads","issues_url":"https://api.github.com/repos/ManageIQ/manageiq/issues{/number}","pulls_url":"https://api.github.com/repos/ManageIQ/manageiq/pulls{/number}","milestones_url":"https://api.github.com/repos/ManageIQ/manageiq/milestones{/number}","notifications_url":"https://api.github.com/repos/ManageIQ/manageiq/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ManageIQ/manageiq/labels{/name}","releases_url":"https://api.github.com/repos/ManageIQ/manageiq/releases{/id}","deployments_url":"https://api.github.com/repos/ManageIQ/manageiq/deployments","created_at":"2014-06-13T02:31:26Z","updated_at":"2017-11-27T12:23:55Z","pushed_at":"2017-11-28T01:24:59Z","git_url":"git://github.com/ManageIQ/manageiq.git","ssh_url":"git@github.com:ManageIQ/manageiq.git","clone_url":"https://github.com/ManageIQ/manageiq.git","svn_url":"https://github.com/ManageIQ/manageiq","homepage":"manageiq.org","size":214704,"stargazers_count":561,"watchers_count":561,"language":"Ruby","has_issues":true,"has_projects":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":546,"mirror_url":null,"archived":false,"open_issues_count":634,"forks":546,"open_issues":634,"watchers":561,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ManageIQ/manageiq/pulls/16518"},"html":{"href":"https://github.com/ManageIQ/manageiq/pull/16518"},"issue":{"href":"https://api.github.com/repos/ManageIQ/manageiq/issues/16518"},"comments":{"href":"https://api.github.com/repos/ManageIQ/manageiq/issues/16518/comments"},"review_comments":{"href":"https://api.github.com/repos/ManageIQ/manageiq/pulls/16518/comments"},"review_comment":{"href":"https://api.github.com/repos/ManageIQ/manageiq/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ManageIQ/manageiq/pulls/16518/commits"},"statuses":{"href":"https://api.github.com/repos/ManageIQ/manageiq/statuses/bd7eaa5255b75dbb5d222d6a4ea0aaf2446304fe"}},"author_association":"MEMBER"}}
{"actor":{"display_login":"kbrock"}}