From 26fd98d7c1ddbeabd49e994e379704197e0ba51c Mon Sep 17 00:00:00 2001 From: Emerson Huitt Date: Thu, 7 Apr 2016 12:43:36 -0400 Subject: [PATCH] Remove existing selects from relation If there are existing, non-standard selects on a relation, the primary key sub-select will fail because it will include them as well as the primary key, which results in too many columns being present in the results. This change removes any existing select clauses before applying the selection on the primary key. --- lib/acts_as_taggable_on/taggable/collection.rb | 2 +- spec/acts_as_taggable_on/taggable_spec.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/acts_as_taggable_on/taggable/collection.rb b/lib/acts_as_taggable_on/taggable/collection.rb index 014d97e45..786623fdc 100644 --- a/lib/acts_as_taggable_on/taggable/collection.rb +++ b/lib/acts_as_taggable_on/taggable/collection.rb @@ -138,7 +138,7 @@ def generate_tagging_scope_in_clause(tagging_scope, table_name, primary_key) scoped_ids = pluck(table_name_pkey) tagging_scope = tagging_scope.where("#{ActsAsTaggableOn::Tagging.table_name}.taggable_id IN (?)", scoped_ids) else - tagging_scope = tagging_scope.where("#{ActsAsTaggableOn::Tagging.table_name}.taggable_id IN(#{safe_to_sql(select(table_name_pkey))})") + tagging_scope = tagging_scope.where("#{ActsAsTaggableOn::Tagging.table_name}.taggable_id IN(#{safe_to_sql(except(:select).select(table_name_pkey))})") end tagging_scope diff --git a/spec/acts_as_taggable_on/taggable_spec.rb b/spec/acts_as_taggable_on/taggable_spec.rb index 97283eb7a..cb7de2757 100644 --- a/spec/acts_as_taggable_on/taggable_spec.rb +++ b/spec/acts_as_taggable_on/taggable_spec.rb @@ -119,6 +119,21 @@ expect(@taggable.tag_counts_on(:tags).length).to eq(2) end + context 'tag_counts on a collection' do + context 'a select clause is specified on the collection' do + it 'should return tag counts without raising an error' do + expect(TaggableModel.tag_counts_on(:tags)).to be_empty + + @taggable.tag_list = %w(awesome epic) + @taggable.save + + expect { + expect(TaggableModel.select(:name).tag_counts_on(:tags).length).to eq(2) + }.not_to raise_error + end + end + end + it 'should have tags_on' do expect(TaggableModel.tags_on(:tags)).to be_empty