How does ActiveRecord do it?

Discussion in 'Ruby' started by Ralph Shnelvar, Nov 30, 2009.

  1. 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?
     
    Ralph Shnelvar, Nov 30, 2009
    #1
    1. Advertising

  2. Ralph Shnelvar

    Matt H Guest

    On Mon, Nov 30, 2009 at 8:02 AM, Ralph Shnelvar <> 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.
    --
    Find me - http://www.smajn.net/social
     
    Matt H, Nov 30, 2009
    #2
    1. Advertising

  3. MH> On Mon, Nov 30, 2009 at 8:02 AM, Ralph Shnelvar <> 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?
     
    Ralph Shnelvar, Nov 30, 2009
    #3
  4. >
    > 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
     
    Markus Roberts, Nov 30, 2009
    #4
  5. Ralph Shnelvar

    Ryan Davis Guest

    On Nov 30, 2009, at 07:51 , Ralph Shnelvar 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.
     
    Ryan Davis, Nov 30, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Devraj

    ActiveRecord for Python/MySQL

    Devraj, Aug 26, 2007, in forum: Python
    Replies:
    3
    Views:
    977
    =?iso-8859-1?q?Luis_M=2E_Gonz=E1lez?=
    Aug 27, 2007
  2. maheshexp
    Replies:
    13
    Views:
    2,088
    ipolevoy
    Jul 23, 2010
  3. Carl Youngblood

    ActiveRecord comments

    Carl Youngblood, Jul 11, 2004, in forum: Ruby
    Replies:
    2
    Views:
    151
    Florian Weber
    Jul 11, 2004
  4. Brian McCallister

    ActiveRecord PostgreSQL patch

    Brian McCallister, Jul 16, 2004, in forum: Ruby
    Replies:
    1
    Views:
    110
    David Heinemeier Hansson
    Jul 16, 2004
  5. Eduardo Scoz
    Replies:
    10
    Views:
    211
    Eduardo Scoz
    Jan 17, 2007
Loading...

Share This Page