Sort in Ascending Order Rails Sort in Ascending Order Rails ruby-on-rails ruby-on-rails

Sort in Ascending Order Rails


Something like this should do the trick...

ItemType.includes( :item ).order( 'inventory_items.name DESC' )

Also, if you need to do this in many locations, you can accomplish the same thing by providing an :order parameter to your has_many call, instead - http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_many.


To retrieve records from the database in a specific order, you can use the order method:

Item.order(:name)

by default this sorts ascending.


For making ASC (Default sorting mode) for name kind of fields (Alphabets),

You can use ORDER BY Clause in MySQL

Hence, In Rails you can simply use

Model.order(:field_name)