ActiveRecord Model Class Name Clash
I’ve just spent way too long trying to work out why the validation callbacks were getting called twice on one of my ActiveRecord models. I thought I’d write this up in case it saves someone else some pain.
In the end, I narrowed the problem down to the name of the model class which is
Sync. I should’ve thought of this sooner, but it turns out there is a Ruby standard library class called
Sync. I haven’t delved any further into why validation was being called twice. As far as I’m concerned, once you re-open an existing Ruby class like this, it makes sense that all bets are off.
However, I am a bit confused why the original Sync model class definition didn’t give a “superclass mismatch”
TypeError, which is what happens if I do :-
script/runner "class Sync < ActiveRecord::Base; end"
It must be a load order issue, since if I put the class definition in a file in app/models, I don’t see the
TypeError. It would’ve saved a lot of time if my
Sync model class definition had failed fast in this way. I might see if there’s a way to make that happen.