# What does Reloadable mean in rails source code?

**URL:** <https://www.ruby-forum.com/t/what-does-reloadable-mean-in-rails-source-code/83723>\
**Category:** Rails\
**Created:** [January 29, 2007, 7:35am UTC](https://www.ruby-forum.com/t/what-does-reloadable-mean-in-rails-source-code/83723 "2007-01-29T07:35:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fredzhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9fc29f/32.png) [@Fredzhang](https://www.ruby-forum.com/u/Fredzhang)\
**Post date:** [January 29, 2007, 7:35am UTC](https://www.ruby-forum.com/t/what-does-reloadable-mean-in-rails-source-code/83723/1 "2007-01-29T07:35:26Z")

</div>

In the actionpack-1.12.5\lib\action\_controller\base.rb,  
you can see the definition:  
class Base

```
include Reloadable::Subclasses

```

…

And following is the definition of Reloadable:

# Classes that include this module will automatically be reloaded

# by the Rails dispatcher when Dependencies.mechanism = :load.

module Reloadable  
class \<\< self  
def included(base) #nodoc:  
raise TypeError, “Only Classes can be Reloadable!” unless  
base.is\_a? Class

```
  unless base.respond_to?(:reloadable?)
    class << base
      define_method(:reloadable?) { true }
    end
  end
end

def reloadable_classes
  included_in_classes.select { |klass| klass.reloadable? }
end

```

end

# Captures the common pattern where a base class should not be

reloaded,

# but its subclasses should be.

module Subclasses  
def self.included(base) #nodoc:  
base.send :include, Reloadable  
(class \<\< base; self; end).send(:define\_method, :reloadable?) do  
base != self  
end  
end  
end  
end

Can anybody here explain how “include Reloadable::Subclasses” works

---

<div class="post-metadata">

**Author:** ![Fredzhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9fc29f/32.png) [@Fredzhang](https://www.ruby-forum.com/u/Fredzhang)\
**Post date:** [January 29, 2007, 9:41am UTC](https://www.ruby-forum.com/t/what-does-reloadable-mean-in-rails-source-code/83723/2 "2007-01-29T09:41:08Z")

</div>

On Jan 29, 2007, at 7:35 AM, Fredzhang wrote:

> In the actionpack-1.12.5\lib\action\_controller\base.rb,  
> you can see the definition:  
> class Base
> 
> ```
> include Reloadable::Subclasses
> 
> ```

In Rails pre-1.2 (in 1.2 this has been revised, I need to reread the  
code), the Reloadable module marked classes that we wanted to be  
reloaded if reloading was active. But sometimes you don’t want your  
very class to be reloadable, but its subclasses.

Models and controllers are reloaded in development mode because  
ActiveRecord::Base and ActionController::Base mixin  
Reloadable::Subclasses:

module ActiveRecord  
…  
class Base  
…  
include Reloadable::Subclasses  
…  
end  
…  
end

The rationale here is that Rails evolves fast but not _that_  
fast :-), so there’s no point reloading ActiveRecord::Base itself in  
each request. With that little trick you skip reloading the framework  
AND free the programmer from having to mark each model and controller.

When you include Reloadable in a class, it gets a class method called  
reloadable? that returns true.

unless base.respond\_to?(:reloadable?)  
class \<\< base  
define\_method(:reloadable?) { true }  
end  
end

When you include Reloadable::Subclasses in a class it gets a class  
methods called reloadable? that returns true unless the class is the  
one where the method was defined. That’s the clousure:

module Subclasses  
def self.included(base) #nodoc:  
base.send :include, Reloadable  
(class \<\< base; self; end).send(:define\_method, :reloadable?) do  
base != self  
end  
end  
end

And so, when Rails wants to reload the part of the application that  
is reloadable, it iterates over all classes in the ObjectSpace, and  
asks to those classes whether they respond to reloadable? In that  
case Rails send :reloadable? and the constant is removed if the  
method returns true.

That happens at the end of each request (if reloading is turned on).  
As you see the way to get reloading is to remove the constant, if we  
just reinterpreted the file we would be _reopening_ the class, which  
is not the same thing. Thus, the very reloading is delegated again to  
const\_missing.

– fxn

---

<div class="post-metadata">

**Author:** ![Fredzhang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9fc29f/32.png) [@Fredzhang](https://www.ruby-forum.com/u/Fredzhang)\
**Post date:** [January 29, 2007, 11:39am UTC](https://www.ruby-forum.com/t/what-does-reloadable-mean-in-rails-source-code/83723/3 "2007-01-29T11:39:29Z")

</div>

Xavier :

> > In the actionpack-1.12.5\lib\action\_controller\base.rb,  
> > you can see the definition:  
> > class Base
> > 
> > ```
> > include Reloadable::Subclasses
> > 
> > ```
> 
> In Rails pre-1.2 (in 1.2 this has been revised, I need to reread the  
> code),

Reloadable mechanism is deprecated in 1.2. If my understanding is  
correct, this is now handled by the Dependencies new code.

– Jean-François.

–  
À la renverse.
