Thursday, December 6, 2007

Use irb when rails is too heavy

The user_pictures table has multiple rows for each thumbnail size we are using. We are using attachment_fu for picture uploads. Due to programmer stupidity we only added the other attribute data (like titles, descriptions, etc) to one of the thumbnail rows. How to propagate the existing data to their companion rows after hundreds of pictures had already been uploaded by users?

Tried script/console but the update action triggered attachment_fu to re-get the size of the referenced picture. Soooo sloooooow. In fact, too slow to run practically (more than a couple hours). Also tried mysql but got caught up in attempting an UPDATE with a sub-query, and failed to get the syntax right for an UPDATE with a self-referential quasi-join. Note to self - give this stuff to the dba.

Finally used plain old irb. Opened an irb session and the first line was: require 'config/environment'. This loaded ActiveRecord and all the configurations needed to work with the existing models, but without the cruft of the rest of the rails app. The ruby to update all the rows appropriately was trivial. Took all of 15 seconds to complete.

I love ruby. And rails. But ruby more.

No comments: