Saving child attributes via nested_attributes doesn't automatically touch the parent?

I “discovered” some interesting functionality today. Let’s say you have the following relationship:

class User
has_many :posts
accepts_nested_attributes_for :posts
end

class Post
belongs_to :user, :touch => true
end

``

Editing a post as a child of a user touches the user record as expected:

user = User.first
user.posts.first.update(:title => “New Post Title”)

``

But submitting that same change via nested attributes does NOT touch the user:

user = User.first
user.update(:posts_attributes => { ‘0’ => { :id => user.posts.first.id, :title => ‘New Post Title’ } })

``

This seems really strange to me given the focus of Russian Doll Caching. Changing a child, in whatever manner you decide to do it, should touch the parent so cache keys update and caching Just Works™. Right?

Unless I’m missing something? I found one blog post on the entire internet that explicitly points out this functionality: http://www.software-thoughts.com/2014/03/rails-updating-association-through.html The single comment on that post says “just use :touch => true” which is what I assumed worked as well!

Rob

Oh snap, it looks like the issue I’m seeing is caused by the paper_trail gem! I extracted my example above from my existing app but removed everything but the essentials. Once I took a look and started commenting out stuff in my User model I found that as soon as I commented out

has_paper_trail

``

the parent was being touched as expected!

I’ll update once I look into it.

Rob

Back to Rails!

https://github.com/rails/rails/issues/26726

TL;DR Adding a before/after_commit/rollback hook causes the parent not to be touched when saving via nested attributes.