Rails models reload the middle request, resulting in a TypeMismatch Association

We encountered a problem in Rails 3.1 when starting in development mode. It seems that our models sometimes get a reloaded middle request, and the new class object_id is set in our model classes. Which then leads to ActiveRecord :: AssociationTypeMismatch

ActiveRecord :: Symbol AssociationTypeMismatch (# 2194222580) expected to receive a symbol (# 2185863000)

If we go to config.cache_classes = true in development.rb , the problem seems to disappear, but it is unrealistic to develop the way we have to constantly restart our servers.

Does anyone have an idea why models can be reloaded with an average request, or if there is a way we could get the cache to serve the entire request?

+7
source share
2 answers

With config.cache_classes = false, any change to the model causes a reboot. This includes defining / overriding a constant defined in a / known model.

We had this problem using rspec and ActsAsFu. Overriding the Fu class during the test caused the related (even indirectly related) classes to reload, and we got an ActiveRecord :: AssociationTypeMismatch error for the associated object. We understood this because we had tests that went fine alone, but failed when working after other tests. Our solution was to simply create separately named Fu classes for each configuration and avoid overriding the class name during the test.

So my recommendation is to make sure that you do not override any constants known to your character class (or known to classes known to your character class, etc.).

+1
source

In the past, I found that reopening (monkey patch) the ActiveRecord model will actually reload the entire class from top to bottom. Try to find your code base for more than one instance of class Character .

0
source

All Articles