samedi 29 août 2015

Rails order product price ascending and descending by their minimum value has_many relation

I am trying to order my products by their price ascending and descending, but depending on the minimum value.

products `has_many` product_prices through product_offers.

I am using @products.includes(:product_offers).includes(:product_prices).where(...).order("product_prices.price ASC") or DESC function of rails, but I need to order the products regarding their minimum prices. I then want to loop like this:

<% for @product in @products %>
 <%=@product.product_prices.minimum("price").first%>
<% end %>

Thats why @products has to have the right order. It is giving me always the same order. What am I doing wrong? The problem is lets say product 1 is in a price range of 10$ - 50$ and product 2 from 20$ - 40$. if I use the order ASC function it would show product 1 then product 2. If i use the order DESC function it would show product 1 then product 2, because 50$>40$, but what I want is 20$> 10$ (Minimums) product 2 then product 1.

Thank you very much for help!

Aucun commentaire:

Enregistrer un commentaire