Ruby on Rails Thursday, October 6, 2016

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



On Thursday, October 6, 2016 at 9:23:50 AM UTC-7, Rob Cameron wrote:
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/64ee0325-d198-4844-a12a-9aa6f5f7a176%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment