Weird Rails behavior with has_many through

I spent the better part of today struggling with Rails while upgrading some old code. Documenting this here in order to hopefully save others the time.

In a case where you have three tables, e.g. Wizard, Dungeon, and DungeonWizard, where DungeonWizard is the join table, you cannot make the foreign keys in DungeonWizard constrained to not null.

In the below code, if you run these statements:

The output is:

Notice the INSERT followed by the UPDATE? This breaks when there is a null constraint on the join table.

Code:

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>