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 57e65a0a2..e1344231c 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