How to search Products by multiple Taxon_ids with Ransack?

299 views
Skip to first unread message

Lorenc Bushati

unread,
May 6, 2014, 11:58:41 AM5/6/14
to spree...@googlegroups.com
Hi all, I'm new in Ruby Rails and Spree,

I'm trying to customize the admin search products of Spree, with non success right now. We need to search all products that have the taxons ids that I pass as params but with Ransack I'm able to searh only by one taxon_id per time, if I pass more than one id, ransack takes only the last one, I have to do an INTERSECT of all results by the different ids, SQL for i.e.:

(SELECT spree_products.* 
FROM spree_products LEFT OUTER JOIN spree_products_taxons 
ON spree_products_taxons.product_id = spree_products.id LEFT OUTER JOIN spree_taxons 
ON spree_taxons.id = spree_products_taxons.taxon_id
WHERE spree_products.deleted_at IS NULL 
AND spree_products.name ILIKE '%white%' 
AND spree_products.state = 'Private'
AND spree_products_taxons.taxon_id = 94)
INTERSECT
(SELECT spree_products.* 
FROM spree_products LEFT OUTER JOIN spree_products_taxons 
ON spree_products_taxons.product_id = spree_products.id LEFT OUTER JOIN spree_taxons 
ON spree_taxons.id = spree_products_taxons.taxon_id 
WHERE spree_products.deleted_at IS NULL 
AND spree_products.name ILIKE '%white%' 
AND spree_products.state = 'Private'
AND spree_products_taxons.taxon_id = 90)

Is there any chance to do it with the use of Ransack?

I'm able to do it with activerecord not using ransack, but this way I have to change all my views, cause "No Ransack::Search object was provided to search_form_for!".

Thank you in advance!
Lorenc

Ryan Bigg

unread,
May 6, 2014, 8:57:13 PM5/6/14
to spree...@googlegroups.com
We have a search helper for this with Spree: Spree::Product.in_taxons(taxon1, taxon2). Try that and see how you go :)
--
Ryan Bigg
Community Manager
Spree Commerce, Inc.

Lorenc Bushati

unread,
May 8, 2014, 5:38:01 AM5/8/14
to spree...@googlegroups.com
Thank you for your quick reply,

we have 3 different taxonomies (Category, Brand, Tag), and we would like to use taxons from these 3 taxonomies in a search, so for example we would like to find products that have a category "Fashion" and a brand "Dior" and a tag "Red".
The problem is that with ransack search we are able to find all products that have at least one of these taxons, but instead we would like to find products that have all these 3 taxons.
 
The mentioned method (Spree::Product.in_taxons(taxon1, taxon2)) is useful to search products that have taxons and children of taxons passed, it is a search with or, and seems not using ransack but activerecord.

If somebody have any idea, please let me know.

Thanks a lot,
Lorenc

Ryan Bigg

unread,
May 8, 2014, 10:00:46 PM5/8/14
to spree...@googlegroups.com
Hi Lorenc,

I don't know how to do this with Spree's built-in search. I would recommend researching ElasticSearch or something similar to add these advanced search features to your Spree store.

Lorenc Bushati

unread,
May 9, 2014, 5:07:21 AM5/9/14
to spree...@googlegroups.com
Hello, 
thank you again for the answers.

I will try.

Lorenc
Reply all
Reply to author
Forward
0 new messages