rentzsch.com: tales from the red shed

Rails habtm bug

Bugs

Beware having a Rails ActiveRecord model class with more than one has_and_belongs_to_many (habtm) relationship. Rails 1.0 has a bug where all non-first habtm’s will not get properly saved to the database (the join table’s rows never get inserted). A simple application illustrating the bug is here.

The bug is known and already fixed in Rail’s stable branch. Until Rails 1.0.1 or 1.1 ships, you can repair it yourself in your own app thanks to Ruby’s open classes. Just replace the broken add_multiple_associated_save_callbacks method and stick it into your ApplicationController’s application.rb file.

The multiple_habtm bug reproduction app has that fix in it. If you care, run the tests and watch it fail. Then open developer_test.rb, set work_around_habtm_bug to true, save and re-run the test (which should pass).

Saturday, February 11, 2006
11:08 PM