# How does ActiveRecord do it?

**URL:** <https://www.ruby-forum.com/t/how-does-activerecord-do-it/179617>\
**Category:** Ruby\
**Created:** [November 30, 2009, 4:02pm UTC](https://www.ruby-forum.com/t/how-does-activerecord-do-it/179617 "2009-11-30T16:02:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 30, 2009, 4:02pm UTC](https://www.ruby-forum.com/t/how-does-activerecord-do-it/179617/1 "2009-11-30T16:02:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 30, 2009, 4:31pm UTC](https://www.ruby-forum.com/t/how-does-activerecord-do-it/179617/2 "2009-11-30T16:31:37Z")

</div>

On Mon, Nov 30, 2009 at 8:02 AM, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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](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.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 30, 2009, 4:52pm UTC](https://www.ruby-forum.com/t/how-does-activerecord-do-it/179617/3 "2009-11-30T16:52:06Z")

</div>

MH\> On Mon, Nov 30, 2009 at 8:02 AM, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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\> [Rails::Configuration](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?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 30, 2009, 8:41pm UTC](https://www.ruby-forum.com/t/how-does-activerecord-do-it/179617/4 "2009-11-30T20:41:37Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 30, 2009, 10:39pm UTC](https://www.ruby-forum.com/t/how-does-activerecord-do-it/179617/5 "2009-11-30T22:39:52Z")

</div>

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.
