Rails columns on demand plugin

will.bryant

I've extracted out the code I was using to lazily load large BLOB/TEXT columns in one of my projects, tidied it up to take advantage of a refactoring in Rails 2.2.2 (cheers Koz) and improve its semantics, and released it as the columns_on_demand plugin.

This plugin is useful if you have large columns on your models but find that you don't need those columns to do most of the things you do with the records. In this case you incur a substantial penalty in memory use (in your appservers) for all the unnecessarily-loaded data, plus a noticable penalty on your database server as it has to do extra seeks to get the off-page large data. With this plugin you can essentially transparently move those columns out so that they are loaded only when they're actually required, without having to manually split your model or put in :select options everywhere.

All you have to do is invoke it in your model:

class MyModel < ActiveRecord::Base
  columns_on_demand
end

And if will lazily load the model's BLOB and TEXT columns from that point on. You can also specify which columns you want (of course, this works with all types of columns, not just :binary/:text):

class MyUpload < ActiveRecord::Base
  columns_on_demand :file_data, :processing_log
end