Ruby on Rails Thursday, October 6, 2016

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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/e916b76f-746a-4754-86b4-4862c34d5ce6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment