You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My goal is to make Article.search(categories_id_in: [1,2,3]) returns articles having all three categories. This was asked in #112, #187 and someotherplaces.
While the in and in_any predicates are working out of the box <plural_association_id>_in_all produces a wrong query as you can see here:
This is correct, produces articles with any of the given categories
1]pry(main)> Article.search(categories_id_in: [1,2,3]).result.to_sql# => "SELECT `articles`.* FROM `articles`LEFTOUTERJOIN`articles_categories`ON`articles_categories`.`article_id` = `articles`.`id` LEFT OUTER JOIN `categories` ON `categories`.`id` = `articles_categories`.`category_id` WHERE `categories`.`id` IN(1,2,3)"
This is also correct, produces same result as above with a verbose query
[2]pry(main)> Article.search(categories_id_in_any: [1,2,3]).result.to_sql# => "SELECT `articles`.* FROM `articles`LEFTOUTERJOIN`articles_categories`ON`articles_categories`.`article_id` = `articles`.`id` LEFT OUTER JOIN `categories` ON `categories`.`id` = `articles_categories`.`category_id` WHERE ((`categories`.`id` IN (1) OR `categories`.`id` IN (2) OR `categories`.`id` IN(3)))"
This is clearly wrong, there cannot be a single row with category_id equal to 1, 2 and 3 at the same time
[3]pry(main)> Article.search(categories_id_in_all: [1,2,3]).result.to_sql# => "SELECT `articles`.* FROM `articles`LEFTOUTERJOIN`articles_categories`ON`articles_categories`.`article_id` = `articles`.`id` LEFT OUTER JOIN `categories` ON `categories`.`id` = `articles_categories`.`category_id` WHERE ((`categories`.`id` IN (1) AND `categories`.`id` IN (2) AND `categories`.`id` IN(3)))"
I was able to obtain the right results by doing this query
[5]pry(main)> Article.search(categories_id_in:[526,527,556]).result.group('articles.id').having('COUNT(categories.id) = ?',3).to_sql# => "SELECT `articles`.* FROM `articles`LEFTOUTERJOIN`articles_categories`ON`articles_categories`.`article_id` = `articles`.`id` LEFT OUTER JOIN `categories` ON `categories`.`id` = `articles_categories`.`category_id` WHERE `categories`.`id` IN(526,527,556)GROUPBYarticles.idHAVINGCOUNT(categories.id)=3"
Now I'd like to try to embed the part group('articles.id').having('COUNT(categories.id) = ?', 3) into a ransacker, however inside ransacker block I do not have access to the array arguments and I cannot build the having part.
Is this something that should be added to the ransacker api?
Is there any other way to accomplish this?
The text was updated successfully, but these errors were encountered:
I have the same problem as fabn... I've read #513, but don't understand how that solves his question. Is there some documentation available on querying HABTM relationship for records that have each of the specified related records (and not OR like in fabn's example queries 1 and 2)
I'm investigating on how to deal with HABTM associations, here's my use case
My goal is to make
Article.search(categories_id_in: [1,2,3])
returns articles having all three categories. This was asked in #112, #187 and some other places.While the
in
andin_any
predicates are working out of the box<plural_association_id>_in_all
produces a wrong query as you can see here:This is correct, produces articles with any of the given categories
This is also correct, produces same result as above with a verbose query
This is clearly wrong, there cannot be a single row with category_id equal to 1, 2 and 3 at the same time
I was able to obtain the right results by doing this query
Now I'd like to try to embed the part
group('articles.id').having('COUNT(categories.id) = ?', 3)
into a ransacker, however inside ransacker block I do not have access to the array arguments and I cannot build thehaving
part.Is this something that should be added to the ransacker api?
Is there any other way to accomplish this?
The text was updated successfully, but these errors were encountered: