rails

Rails 3.2.9 through associations issues

| 2 Comments

Recent 3.2.9 release bugfix has brought an issue for through associations. inverse_of usage can solve the problem only partially (polymorphic associations are still affected).

UPDATE: patch that introduced this issue has just been reverted in rails master.

Few days ago Rails 3.2.9 was released. Among the bugfixes a fix for through associations is present, and since 3.2.9 join model instances are built even though the object is not saved yet.

To get the point take a look at these simple models with User and Team joined with each other through UserMembership:

Before Rails 3.2.9 this code does not build any UserMembership instances:

But since Rails 3.2.9 UserMembership instance is built:

Oh, have you noticed that team is invalid now? Aahh, now when it get’s complicated.

Built instance of a join UserMembership model has no team set and that’s why it’s invalid (it has validate :team, :user, presence: true). As soon as join model instance is invalid team is invalid as well.

To fix this validation issues in this particular case the one should use inverse_of for the associations like this:

inverse_of takes care of associations objects in-memory synchronization, so now UserMembership instance has a team and it’s valid. Unfortunately polymorphic associations are still affected – follow this github discussion for more info.

That said, if you’ve just upgraded to 3.2.9 and your tests are failing review the associations and fix them whenever it’s possible by using inverse_of.

Share
* Railsware is a premium software development consulting company, focused on delivering great web and mobile applications. Learn more about us.
  • Pingback: Rails 3.2.9 through associations issues | My Daily Feeds

  • Ray

    Good to know that associations are build now as well as the problems with it. All in all it seems less than a bug, but a not clearly specced out feature.

    Hopefully this area will see some more love, than I can remove a couple of dirty hacks in some association code.

Want to get more of Railsware blog?

RSS FEED

We're always ready to help!

CONTACT US