Extending ActiveRecord::Base broken during 2.2.0 RC update


#1

I’m trying to diagnose some breakage that occurred in the update from
2.1.2 to 2.2.0.

The short version: an “acts_as_searchable” method added to
ActiveRecord::Base doesn’t work anymore (raises NoMethodError).

The long version:

The method is added by a file inside the “lib” directory.

The file is “required” from “config/environment.rb”.

If I print a debug messages from “config/environment.rb” I can see
that the method was indeed added to ActiveRecord::Base; ie, the
following prints “true”:

p ActiveRecord::Base.methods.include?(‘acts_as_searchable’)

If I print a debug message from inside a model file, the same
statement prints “false”.

So somewhere between the evaluation of “config/environment.rb” and the
evaluation of the model file which uses the “acts_as_searchable”
method, the method is actually getting removed from
ActiveRecord::Base.

I am still digging around trying to find out why this is happening.
Anyone have any suggestions or ideas?

Cheers


#2

On 19 Nov 2008, at 10:47, Wincent C. wrote:

So somewhere between the evaluation of “config/environment.rb” and the
evaluation of the model file which uses the “acts_as_searchable”
method, the method is actually getting removed from
ActiveRecord::Base.

I am still digging around trying to find out why this is happening.
Anyone have any suggestions or ideas?

I suspect that the difference is that in production mode on 2.2 models
are preloaded at the end of initialisation process, which is before
the stuff at the end of environment.rb gets run, ie your models are
loaded before you’ve required the file adding the stuff.

Fred


#3

On 19 nov, 11:57, Frederick C. removed_email_address@domain.invalid wrote:

Anyone have any suggestions or ideas?

I suspect that the difference is that in production mode on 2.2 models
are preloaded at the end of initialisation process, which is before
the stuff at the end of environment.rb gets run, ie your models are
loaded before you’ve required the file adding the stuff.

Thanks, Fred. Makes sense, but it doesn’t explain the order in which I
see the debug statements get logged…

  1. “inside environment.rb”
  2. “requiring lib file”
  3. “ActiveRecord::Base now includes the desired method”
  4. “evaluating model file”
  5. “ActiveRecord::Base does not include the desired method”

Must be some kind of anomaly caused by buffering or something which
makes the statements get echoed out of order (I haven’t turned multi-
threading on so I understand that this should all be happening in a
single thread).

This is actually in the test environment because I’m testing this by
doing “rake spec”.

I tried “script/server” in the development environment and it works;
tried it in production and I get the same failure.

I then added the following to config/environments/test.rb and config/
environments/production.rb and those environments now work again.

config.cache_classes = false

So it works, but it seems like a bad idea to deploy with that
configuration due to the performance hit of reloading the classes with
every request.

I suppose there must be a better solution. Any suggestions?

Cheers,
Wincent


#4

On 19 Nov 2008, at 11:18, Wincent C. wrote:

I then added the following to config/environments/test.rb and config/
environments/production.rb and those environments now work again.

A better way could be to move the requires from your environment.rb
into an initializer in config/initializers - this would mean they’re
called at an appropriate moment in the boot process.

config.cache_classes = false

So it works, but it seems like a bad idea to deploy with that
configuration due to the performance hit of reloading the classes with
every request.

Yeah that would be a super bad idea

Fred