ActiveRecord Model Class Name Clash

Posted by James Mead Mon, 02 Nov 2009 16:21:44 GMT

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.

Tags , , , , , , ,  | 1 comment

Comments

  1. Jeremy McAnally said about 9 hours later:

    It has to do with the way Rails loads constants (if it already exists, it won’t load it, etc. etc. etc.). It’s a lot of mumbo-jumbo that usually complains if something like this happens, but it seems you might’ve fallen into an edge case or something. :)

Comments are disabled