Showing posts with label db sql activerecord. Show all posts
Showing posts with label db sql activerecord. Show all posts

Wednesday, October 15, 2008

Sorting records based on the exact order of passed parameters to finder

http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/dde7efa8f8f455e5/e4090afeb6c27608?lnk=gst&q=loveajax#e4090afeb6c27608

In Ruby:
order = [5,2,3]
Project.find(:all, :conditions => ["id IN (?)", order], :order =>
"field(id, #{order.join(",")})")

In MySQL:
SELECT * FROM projects WHERE (projects.`id` IN (1,2,3,5,7,6)) ORDER BY field(id, 5,1,2,3,7,6);