Rails activerecord get Model. all using an order by

By default, when you do Model.all in rails, it returns data based on what­ever order was returns by the data­base. But some­times thats not what you want.

For exam­ple, if I had a model called ‘Prod­uct’ and wanted to retrieve all the prod­ucts by order_date, here’s what you need to write in your controller.

Product.order('order_date DESC');

Ryan Sukale

Ryan is a UX engineer living in San Francisco, California.

You may also like...