Resolving callback issues with ActiveRecord after upgrading Rails 5.2 to 6.1
I'm writing unit tests and I'm stuck on something that should probably be simple. Working on a legacy Ruby on Rails application, I've run into a perplexing issue after upgrading from Rails 5.2 to 6.1. Everything seemed to go smoothly during initial testing, but upon deploying to production, I noticed that some ActiveRecord callbacks are not triggering as expected. In particular, I have a model, `User`, with a `before_save` callback that sends a welcome email: ```ruby class User < ApplicationRecord before_save :send_welcome_email def send_welcome_email # Logic to send email end end ``` In the past, this callback worked flawlessly, but now, it appears that the method isn't being called during user creation. When I dug into the logs, I can see that the `User` instance is being saved, but there’s no indication that the callback is firing. I've tried a few different approaches: 1. **Reverting the Rails version** to 5.2 temporarily showed that the callback works again. This confirmed that the upgrade was the culprit. 2. **Checking for model validity** right before saving in the console: ```ruby user = User.new(email: 'test@example.com') user.valid? # => true user.save # This should trigger the callback ``` However, the method still doesn’t execute. 3. **Verifying the method name** for typos or misconfigurations. Everything appears correct, and there’s no issue with the method definition itself. Moreover, I’ve looked into the differences in callback behavior introduced in Rails 6.1, particularly around `before_validation` and `before_save`. According to the Rails upgrade guide, some behavior has slightly changed, especially when dealing with `touch` on associated records or validations that may prevent the callback from firing. Additionally, I attempted to add debugging statements within the callback method itself, but they don’t appear in the logs. This leads me to wonder if there might be a configuration or a gem that’s conflicting. Using `ActiveSupport::Notifications` to trace the callbacks did not yield useful output either. Has anyone experienced similar issues after upgrading? Any insights on how to fix this or best practices for debugging callback issues in Rails would be immensely appreciated. This issue appeared after updating to Ruby 3.10.