How does ActiveRecord do it?

I know it sounds like a Rails question … but it isn’t.

Consider
irb(main):001:0> class X
irb(main):002:1> puts “Hi”
irb(main):003:1> end
Hi
=> nil
irb(main):004:0> x = X.new
=> #<X:0x42458ac>

The “Hi” is sent to stdout as the class X is parsed. (Well, after the
end statement.) Right?

“Hi” is not generated when a new instance of X is created … right?

So now consider

in .\app\models\user.rb

class X < ActiveRecord::Base
puts “Hi 2”
end

“Hi 2” gets displayed by webrick every time I generate a form.

How does this happen? Is webrick(?) reloading .\app\models\user.rb
every
time a form is generated?

(And now a Rails question …)

Assuming it is reloading .\app\models\user.rb in development mode …
will it do this in production mode?

On Mon, Nov 30, 2009 at 8:02 AM, Ralph S. [email protected]
wrote:

Is webrick(?) reloading .\app\models\user.rb every
time a form is generated?

Yes, well it’s not webrick doing the reloading, it is Rails, Rails
will reload all your models before each request in development mode.
Rails configuration API is found here:

http://api.rubyonrails.org/classes/Rails/Configuration.html

In particular the ‘cache_classes’, attribute which is configured in
the RAILS_ROOT/config/environments/*.rb filles. You can also review
the following files for descriptions of the various options:

  • RAILS_ROOT/config/environment.rb
  • RAILS_ROOT/config/environments/development.rb
  • RAILS_ROOT/config/environments/production.rb
  • RAILS_ROOT/config/environments/test.rb

Assuming it is reloading .\app\models\user.rb in development mode …
will it do this in production mode?

‘cache_classes’ is set to true by default in
RAILS_ROOT/config/environments/production.rb. This disables reloading
in production mode.

Also, when you’re in the rails console, you can manually reload
classes by calling ‘reload!’.

The rails mailing list will be able to provide much more satisfactory
answers regarding such items in the future. You should post rails
related questions there.

MH> On Mon, Nov 30, 2009 at 8:02 AM, Ralph S. [email protected]
wrote:

Is webrick(?) reloading .\app\models\user.rb every
time a form is generated?

MH> Yes, well it’s not webrick doing the reloading, it is Rails, Rails
MH> will reload all your models before each request in development mode.
MH> Rails configuration API is found here:

MH> http://api.rubyonrails.org/classes/Rails/Configuration.html

MH> In particular the ‘cache_classes’, attribute which is configured in
MH> the RAILS_ROOT/config/environments/*.rb filles. You can also review
MH> the following files for descriptions of the various options:

MH> * RAILS_ROOT/config/environment.rb
MH> * RAILS_ROOT/config/environments/development.rb
MH> * RAILS_ROOT/config/environments/production.rb
MH> * RAILS_ROOT/config/environments/test.rb

Assuming it is reloading .\app\models\user.rb in development mode …
will it do this in production mode?

MH> ‘cache_classes’ is set to true by default in
MH> RAILS_ROOT/config/environments/production.rb. This disables
reloading
MH> in production mode.

MH> Also, when you’re in the rails console, you can manually reload
MH> classes by calling ‘reload!’.

MH> The rails mailing list will be able to provide much more
satisfactory
MH> answers regarding such items in the future. You should post rails
MH> related questions there.

The question I was trying to ask was …

What Ruby facility does Rails use to reload the class?

The question I was trying to ask was …

What Ruby facility does Rails use to reload the class?

As a rough answer, Kernel#load and the various const-introspection
methods (to remove the old class first).

The great thing about open source is that if you’d like a more
detailed answer you can just go look at the code and see for yourself.

– Markus

On Nov 30, 2009, at 07:51 , Ralph S. wrote:

What Ruby facility does Rails use to reload the class?

#load will always load a file (contrasted to #require). Rails does a few
other things during reload, but that’s the main mechanism for it.