Skip to content

Commit

Permalink
Remove existing selects from relation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Emerson Huitt committed Apr 7, 2016
1 parent dde41a4 commit 3b03d8c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/acts_as_taggable_on/taggable/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions spec/acts_as_taggable_on/taggable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 3b03d8c

Please sign in to comment.