New Rails plugin: IncludeByDefault

It’s true, I’m a plugin writing machine. Seriously though, this one’s tiny. I took all of five minutes to write. What it does is, it lets you specify a default value for the :include option on ActiveRecord::Base.find, so you can automate eager loading of associations. I’ll use an example I’m comfortable with:

  class BlogEntry < < ActiveRecord::Base
    has_many :photos
    has_many :comments
    include_by_default :photos, :comments
  end

With that in place, the photos and comments will automatically be loaded in the same database query as the entry you ask for. You can still use the :include option with find – it will override the defaults you specify. Install as usual:

script/plugin install
    git://github.com/jcoglan/include_by_default

If you’ve enjoyed this article, you might enjoy my recently published book JavaScript Testing Recipes. It’s full of simple techniques for writing modular, maintainable JavaScript apps in the browser and on the server.