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: Software Thoughts: Rails: updating an association through nested attributes does not touch the owner The single comment on that post says “just use :touch => true” which is what I assumed worked as well!
Rob