bug. before_validation run twice in method Fabricate
Closed this issue · 1 comments
Here is code sample
class User < ActiveRecord::Base
attr_accessor :counter
before_validation :increase_counter, on: :create
def increase_counter
if @counter.nil?
@counter = 1
else
@counter += 1
end
end
end
when run following in test
Fabricate(:user)
the @counter becomes 2.
You can get a rails sample to reproduce the bug in https://github.com/raykin/learn_rails/tree/before-validation-called-twice. Run rake test:models, there will be one test failed and one passed.
After research the source code, I think i got the cause.
On this line
I didn't use callbacks in fabricators, so doesn't understand why Fabrication need another group of callbacks. The problem is I doesn't find a possible solution to only disable before_validation in save! method in Rails. So we may need another solution to inject Fabrication's callbacks to fix this bug?
Interesting. Any chance you could make a PR with a failing test that demonstrates the issue?