How to sort products by a taxon property in Spree

1.1k views Asked by At

The Spree (2.3.4) application I've inherited has a taxonomy named "Makers" with multiple taxons associated to it by 'taxonomy_id'. I have been tasked with sorting products by the taxon's 'name' property, something like

Spree::Product.order('taxon.name DESC')

I think I need to do an includes on the Products table, but I'm not sure how to write that correctly.

How do I include the correct taxonomy in order to sort by a property of its taxons? Thanks.

2

There are 2 answers

0
aperture On BEST ANSWER

Figured it out:

Spree::Product.includes(:taxons).where(spree_taxons: {taxonomy_id: Spree::Taxonomy.find_by(name: 'Makers').id}).order("spree_taxons.name desc")

I didn't know that you could use .where() like that.

8
Caffeine Coder On

You can directly override this controller in your local , and change the attribute you want to sort by . Only pick the method you want to use , instead of picking up the whole controller code . Let me know if i can help you more .

Note - Be sure , the path you override the controller is exactly the same as in github .