Crake Aborted! – Fixing ActiveRecord Join Migrations

When attempting to use has_and_belongs_to_many you might run into a “relation does not exist” error. If you created a join table migration already, and you’re wondering why ActiveRecord isn’t looking at it, you probably used the wrong table name.

Creating Join Relations Correctly

When looking up a habtm join table, ActiveRecord arranges the model names in alphabetical order. So a habtm join table for the models Clubs and People should have a table name of clubs_people. The migration should look like this:

Just as a reference for the future, you should also notice a few more things about this:

Fixing Already Migrated Relations

The above doesn’t help if you’ve already created your table, but the solution is very simple: