Ruby on Rails: how do I sort with two columns using ActiveRecord?

Ruby on-Rails

Ruby on-Rails Problem Overview


I want to sort by two columns, one is a DateTime (updated_at), and the other is a Decimal (Price)

I would like to be able to sort first by updated_at, then, if multiple items occur on the same day, sort by Price.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

In Rails 4 you can do something similar to:

Model.order(foo: :asc, bar: :desc)

foo and bar are columns in the db.

Solution 2 - Ruby on-Rails

Assuming you're using MySQL,

Model.all(:order => 'DATE(updated_at), price')

Note the distinction from the other answers. The updated_at column will be a full timestamp, so if you want to sort based on the day it was updated, you need to use a function to get just the date part from the timestamp. In MySQL, that is DATE().

Solution 3 - Ruby on-Rails

Thing.find(:all, :order => "updated_at desc, price asc")

will do the trick.

Update:

Thing.all.order("updated_at DESC, price ASC")

is the Rails 3 way to go. (Thanks @cpursley)

Solution 4 - Ruby on-Rails

Active Record Query Interface lets you specify as many attributes as you want to order your query:

models = Model.order(:date, :hour, price: :desc)

or if you want to get more specific (thanks @zw963 ):

models = Model.order(price: :desc, date: :desc, price: :asc) 

Bonus: After the first query, you can chain other queries:

models = models.where('date >= :date', date: Time.current.to_date)

Solution 5 - Ruby on-Rails

Actually there are many ways to do it using Active Record. One that has not been mentioned above would be (in various formats, all valid):

Model.order(foo: :asc).order(:bar => :desc).order(:etc)

Maybe it's more verbose, but personally I find it easier to manage. SQL gets produced in one step only:

SELECT "models".* FROM "models" ORDER BY "models"."etc" ASC, "models"."bar" DESC, "models"."foo" ASC

Thusly, for the original question:

Model.order(:updated_at).order(:price)

You need not declare data type, ActiveRecord does this smoothly, and so does your DB Engine

Solution 6 - Ruby on-Rails

Model.all(:order => 'updated_at, price')

Solution 7 - Ruby on-Rails

None of these worked for me! After exactly 2 days of looking top and bottom over the internet, I found a solution!!

lets say you have many columns in the products table including: special_price and msrp. These are the two columns we are trying to sort with.

Okay, First in your Model add this line:

named_scope :sorted_by_special_price_asc_msrp_asc, { :order => 'special_price asc,msrp asc' }

Second, in the Product Controller, add where you need to perform the search:

@search = Product.sorted_by_special_price_asc_msrp_asc.search(search_params)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNullVoxPopuliView Question on Stackoverflow
Solution 1 - Ruby on-RailsChristian FazziniView Answer on Stackoverflow
Solution 2 - Ruby on-RailsDaniel VandersluisView Answer on Stackoverflow
Solution 3 - Ruby on-RailsErik EscobedoView Answer on Stackoverflow
Solution 4 - Ruby on-RailsgolfadasView Answer on Stackoverflow
Solution 5 - Ruby on-RailsRuby RacerView Answer on Stackoverflow
Solution 6 - Ruby on-RailsrobertoklView Answer on Stackoverflow
Solution 7 - Ruby on-RailsMax Alexander HannaView Answer on Stackoverflow