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 = Post.new(title: 'ruby rocks') post.comments.build(body: 'hello world') post.save # => 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:post.comments.build(user_id: user.id) # has_many :comments post.comments.first.post.object_id == post.object_id # => false # has_many :comments, :inverse_of => :post post.comments.first.post.object_id == post.object_id # => true
See also “Bi-directional associations” in Rails API 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 = @post.tags.build :name => "ruby" @tag.save
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 end
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 https://github.com/rails/rails/commit/d6b03a376787ec9c9e934e5688a38c576f2e39b7