mbleigh / acts-as-taggable-on

A tagging plugin for Rails applications that allows for custom tagging along dynamic contexts.
http://mbleigh.lighthouseapp.com/projects/10116-acts-as-taggable-on
MIT License
4.95k stars 1.18k forks source link

`tagged_with` adding backslash to tag names when `any: true` #1116

Open russellbrown opened 8 months ago

russellbrown commented 8 months ago

When I use tagged_with with multiple tags and any: true I always get 0 results and it seems to be due to a backslash getting added to all but the last tag in the SQL query.

Suppose I have tagged a post (id=1) with 'ABC'.

# The post is found as expected
Post.tagged_with(["ABC"], any: true).find(1)
=> #<Article id: 1...

But:

Post.tagged_with(["ABC","DEF","XYZ"], any: true).find(1)
=> ActiveRecord::RecordNotFound

In the last example the SQL query generated is adding backslashes to all but the final tag name:

SELECT  "posts".* FROM "posts" WHERE EXISTS (SELECT * FROM "taggings" WHERE "taggings"."taggable_id" = "posts"."id" AND "taggings"."taggable_type" = 'Post' AND "taggings"."tag_id" IN (SELECT "tags"."id" FROM "tags" WHERE (LOWER("tags"."name") ILIKE 'abc\' ESCAPE '!' OR LOWER("tags"."name") ILIKE 'def\' ESCAPE '!' OR LOWER("tags"."name") ILIKE 'xyz' ESCAPE '!'))) AND "posts"."id" = 1 LIMIT 1;

When I copy and paste that query into the database (psql) console and run it it returns 0 rows. But when I manually remove the backslashes from the first 2 tag names and re-run the query I get the row I was originally expecting.

Is this a bug?

My setup:

ruby 2.4.0 rails 5.2.8 acts-as-taggable-on 8.1.0