Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid select(all_fields) in any tag query to fix error when use as subquery #1049

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@ module Taggable
module TaggedWithQuery
class AnyTagsQuery < QueryBase
def build
taggable_model.select(all_fields)
.where(model_has_at_least_one_tag)
taggable_model.where(model_has_at_least_one_tag)
.order(Arel.sql(order_conditions))
.readonly(false)
end

private

def all_fields
taggable_arel_table[Arel.star]
end

def model_has_at_least_one_tag
tagging_arel_table.project(Arel.star).where(at_least_one_tag).exists
Expand Down
10 changes: 10 additions & 0 deletions spec/acts_as_taggable_on/taggable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,16 @@
expect(TaggableModel.tagged_with('ruby').to_a).to eq(TaggableModel.tagged_with('Ruby').to_a)
end

it "should be able to used in subquery" do
TaggableModel.create(name: 'Bob', tag_list: 'ruby, python')
TaggableModel.create(name: 'Frank', tag_list: 'ruby')

expect(TaggableModel.where(id: TaggableModel.tagged_with('ruby', any: true)).count).to eq(2)
expect(TaggableModel.where(id: TaggableModel.tagged_with('ruby', exclude: true)).count).to eq(0)
expect(TaggableModel.where(id: TaggableModel.tagged_with('ruby', match_all: true)).count).to eq(1)
expect(TaggableModel.where(id: TaggableModel.tagged_with('ruby')).count).to eq(2)
end

it 'should be able to find by tags with other joins in the query' do
@taggable.skill_list = 'ruby, rails, css'
@taggable.tag_list = 'bob, charlie'
Expand Down