before_validation
is supposed to appear before after_commit
.19
20class Person < ApplicationRecord
21 after_commit :after_commit_callback
22 before_validation :before_validation_callback23
24 validates :name, presence: true
25end
Description
ActiveRecord callbacks, such as before_save
, after_save
, etc., are methods that are executed at specific points
in the lifecycle of a model. However, in this case, the callbacks are not being executed in the correct order,
leading to unexpected behavior.
Bad practice
class Person < ApplicationRecord
after_commit :after_commit_callback
before_validation :before_validation_callback
end
Recommended
class Person < ApplicationRecord
before_validation :before_validation_callback
after_commit :after_commit_callback
end