I got an Order
and a Product
model with a Many-to-Many relationship. So an order has_many
products, each of them has different prices.
Now I want to sort the orders with the maximum price of the product that an order has.
Here is my code:
@orders = Order.group(arel_table[:id]).
order(Product.arel_table[:price].maximum)
It works perfectly with the default ASC order, but when I sort it as DESC order like this:
@orders = Order.group(arel_table[:id]).
order(Product.arel_table[:price].maximum.desc)
I got an error like this :
undefined method `desc' for #<Arel::Nodes::Max:0x007fb2ab9104a0>
How can I sort the orders as DESC?