Tuesday, July 22, 2008

numeric




I had a string field in a MySQL database connected to Rails that contained a long series of mostly numbers:


001
002
010
023
100_1
200


And I wanted to order them numerically. I found the answer at How to do natural alpha-numeric sort in MySQL and the code I use is:


@recordings = Recording.paginate :page => params[:page], :per_page => 10, :order => 'tape + 0', :conditions => { :year => params[:id] }


That little:


:order => 'tape + 0'


Is what was needed. Neat!