ruby on rails - Get Product ordered by percent_off -


i have 2 model product , pricinghistory.

product(id, name) has_many pricinghistory (id, percent_off, product_id)

i need join product lastest pricinghistory , order percent_off.

i wrote this:

product.joins(:pricing_histories).group('products.id').order("max(pricing_histories.id) asc").order('max(pricing_histories.percent_off) desc') 

but not returning data in order.

eg: product:

id    product  1       aa 2       bb 3       cc 

pricinghistory

product_id percent_off   created_on    1          10         10/10/2015    1          20         10/11/2015    2          30         10/12/2015    2          40         10/13/2015    3          50         10/14/2015    3          60         10/14/2015 

i want result be:

 id    product  product_id percent_off created_on  1       aa     1          20         10/11/2015  2       bb     2          40         10/13/2015  3       cc     3          60         10/14/2015 

you can achieve follows:

pricinghistory.joins(:product).group('products.id').order('pricing_histories.percent_off desc')


Comments