DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,673 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
mmyoji
mmyoji

Posted on • Updated on

touch triggers other touches

ActiveRecord::Persistence#touch is a useful method and I like it.

But I didn't really understand its spec well and it caused a deadlock in our Rails project. You have to care about its impact.

class Post < ApplicationRecord
  belongs_to :post_category, touch: true
  belongs_to :writer, touch: true
end

# This class is a pseudo class
class SomeClass
  def initialize(post, args)
    @post = post
    # ...
  end

  def call
    # ...
    @post&.touch(:published_at)
    # triggers following:
    # @post.post_category.touch
    # @post.writer.touch
  end
end
Enter fullscreen mode Exit fullscreen mode

In SomeClass#call, Post#touch is called and I thought this only update the post.published_at, but also update post_category.updated_at and writer.updated_at.

The above code is just an example and it might be a trouble when you use touch casually in the real application.

You just care about logs before deployment :)

Top comments (0)

πŸ‘‹ Hey, my name is Noah and I’m the one who set up this ad. My job is to get you to join DEV, so if you fancy doing me a favor, I’d love for you to create an account.

If you found DEV from searching around, here are a couple of our most popular articles on DEV: