The best way to sort an ActiveRecord array is to use the default method method
@ users.order (: created_at)
This is the fastest and most correct solution, because in this case it returns a sorted array from db, and you do not need to use any other operation for this in the class, for example, if you use the proposed sort_by , it will loop every element array, and after that it will not be an ActiveRecord array, not cool in my opinion.
order can use strings and sumbols, it is very useful, and several parameters are required
@ users.order ('created_at asc, first_name desc, last_name asc')
Dmitriy Gusev Feb 10 '17 at 9:15 2017-02-10 09:15
source share