June 26th, 2013

Recently, I wanted to utilize the AutosaveAssociation module which (wrapped in a transaction) autosaves a model record when the parent is saved. I’ll use the example from the api docs:

post = 'ruby rocks') 'hello world') # => saves both post and comment

But because I had a validation on comments that requires presence of post, it was attempting to validate comments before post was created. So instead of saving all my models in one swoop, it threw a validation error.

Wut? This should just work! I fussed with it for hours and the only workaround appeared to be to call save with validation: false.

Ew. Anyone else feel queasy?

Turns out the missing piece was to add inverse_of on my model associations. Here’s an awesome explanation from my coworker @mdaubs83:

The :inverse_of option is needed here to inform AR about the inverse association so that the Comment instance returned from build() can reference the original Post instance. This in turn allows the Comment instance to see the id of the Post instance after it’s saved and update it’s foreign key accordingly when AutosaveAssociation calls save() on associated objects. There are other benefits to using :inverse_of, we should probably consider adding the option to all associations. Here’s evidence of the issue and how adding inverse_of solves it:

# has_many :comments == post.object_id
 # => false

# has_many :comments, :inverse_of => :post == post.object_id
 # => true

See also “Bi-directional associations” in Rails API Docs

Thanks, Matt! Sadly, inverse_of is never mentioned in the AutosaveAssociation docs. But I noticed this line in the ActiveRecord Association docs.

If you are using a belongs_to on the join model, it is a good idea to set the :inverse_of option on the belongs_to, which will mean that the following example works correctly (where tags is a has_many :through association):

@post = Post.first
@tag = :name => "ruby"

The last line ought to save the through record (a Taggable). This will only work if the :inverse_of is set:

class Taggable ActiveRecord::Base
  belongs_to :post
  belongs_to :tag, :inverse_of => :taggings

Err. Okay. Would have been useful to see that little tidbit included in the AutosaveAssociation docs as well!

But good news! Matt also tells me that Rails 4.1 is poised to support automatic inverse_of detection. So once we all upgrade we get inverse_of goodness for free. W00t!

Commit d6b03a3 to rails/rails by wangjohn

Entry Filed under: Professional,Ruby on Rails

Leave a Comment


Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed



@RogueNASA @darkmoves My mother learned English watching Sesame Street with us when I was a kid. That and Electric Company.

About a week ago from Liana Leahy's Twitter via Twitterrific