Natural Alpah Numberic Sort in Rails

Posted by paul, Fri Jan 12 02:54:00 UTC 2007

HitecRCD is a ruby on rails site I recently built for my current employer, achieve internet. One problem that recently came up was that product names are alpha numeric (ie: HS-35HD, HS-45HD, etc.), and need to be sorted as such. I came across this little posting via a quick Google search and voila...

1
2
3
4
5
  has_many :products,
     :class_name => "Product",
     :foreign_key => "product_category_id", 
     :order => 'name + 0 ASC'  
     #before, it was   :order => 'name ASC' 
A small + 0 to my category model was all it took.

What's interesting to me is not really this little MySQL trick, but rather that the first link I click on (3rd on the page) turned up how to solve my problem in rails, when all I searched for was mysql sort numeric. I think it's kind of testament to the thriving rails community and it willingness to share.

Filed Under: | Tags: google MySQL mysql rails RoR ruby Ruby sort |

Comments