The Class ActiveRecord::Base provides an interesting method to execute searches on the database without the need to write SQL queries. It is the find method.
The find method has some retrieval options (:all, :first, :last, :id) and many parameters which are detailed in the links above. In this article we'd like to focus on the :conditions parameter.
It is very easy to use an 'order by' clause in a model in Ruby on Rails. Supposing you have a Category model it's as simple as doing this:
@categories = Category.find(:all, :order => 'title')
But what if I need to order by the title of the subcategories after ordering by the categories title?
Using the category_id does not work either, because it almost sure that the categories will not be added in alphabetical order, so the command below does not work for me either:
Recent comments
3 days 6 hours ago
3 weeks 1 day ago
3 weeks 5 days ago
4 weeks 4 days ago
5 weeks 3 days ago
7 weeks 2 hours ago
8 weeks 5 days ago
14 weeks 22 hours ago
18 weeks 3 days ago
25 weeks 5 days ago