polymorphs 21 harder faster stronger


old content

You can now skip class reloading in model classes referenced by has_many_polymorphs in development mode.

Normally if you modify a model, Rails will unload and reload the file. This means the parent model has to build the polymorphic relationship all over again, which can be slow. But if the child classes aren’t unloaded in the first place, we avoid this problem. Of course while you are working on the models themselves you need to switch the caching off.

how to

In config/environments/development.rb:

config.after_initialize do
  config.has_many_polymorphs_cache_classes = true

That’s all.


The instructions in this section are unnecessary as of version 27.1 of the plugin.

Also at the top of app/controllers/application.rb, you need:

require 'model_that_contains_the_polymorphs_call'

This makes sure the parent model is always loaded at least once. Note that STI parent classes will also get ejected from the reloadable lists, since they need to be kept around for their children to function.

If you are working in the console or migrations and need to access the methods added by the plugin, you still need to manually reference your polymorphic parent class.


The usual place. Rails 1.2.0 or later is required for the new features.

3 responses

  1. Not sure why but it turns out that I still need to put the require 'model_that_contains_the_polymorphs_call'.

    Although the new autoloading gave me @community.members and @user.communities without the require statement, it turns out that all the instance methods defined in the Community model weren’t available to me unless I put the require statement back into application.rb.

  2. Oops. I wrote too fast. Looks like moving the has_many_polymorphs call to the very top of the model file solved it without a custom require. Sorry for the confusion and thanks for getting the autoloading to work!

  3. It shouldn’t matter where the macro call is. Can you reproduce the problem consistently? Send me an email or post on the forum if the issue seems to persist.