Thursday, October 30, 2008

Common MySQL stuff

To modify an existing column definition:

ALTER TABLE applications modify id INTEGER NOT NULL AUTO_INCREMENT FIRST;

To rename a column:

ALTER TABLE applications CHANGE old_col new_col;

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);