Search code examples
ruby-on-railscallbackruby-on-rails-6beforeupdate

Why is Rails before_update callback deprecated? What's the alternative?


I looked at https://apidock.com/rails/ActiveRecord/Callbacks/before_update and it says before_update got deprecated. Does anybody know why that is and what would be the alternative?

I have some code logic that needs to run specifically when an existing record is updated. Before_save is called on both create and update, so I can't use that.

Edit: I guess I can do before_save :do_stuff, if: !self.new_record? but I find this still odd..

Edit2: !self.new_record? inside the if: block doesn't work. I need to put the !self.new_record? logic inside the actual method.


Solution

  • I don't think before_update has been deprecated in Rails 6. I would advise on using api.rubyonrails.org (in this case) as the preferred reference as it more closely follows the API. You can also have a look at the source directly and see that it's still there. Lastly, the latest version of the Rails Guides suggests it is still an available callback. Try using before_update in your Rails 6 application, and see if it works as expected.