由于升级到Rails 6,我无法启动after_rollback回调。我已经验证了这在Rails6.0.3.6
或6.1.3.1
上不起作用。感谢您的帮助!
class User < ActiveRecord::Base
after_rollback do |user|
puts "You have rolled back!"
end
validates_presence_of :name
end
> User.first.update!(name: nil)
ActiveRecord::RecordInvalid (Validation failed: Name cant be blank)
> User.create!
ActiveRecord::RecordInvalid (Validation failed: Name cant be blank)
根据文档,after_rollback
应在创建、更新或销毁被回滚后触发
只有在成功验证要保存的实体后,才会打开默认的DB事务,除非手动打开它们。尝试
User.transaction do
User.first.update!(name: nil)
end