# Preloading a controller just once

**URL:** <https://www.ruby-forum.com/t/preloading-a-controller-just-once/84252>\
**Category:** Rails\
**Created:** [February 2, 2007, 6:33pm UTC](https://www.ruby-forum.com/t/preloading-a-controller-just-once/84252 "2007-02-02T18:33:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [February 2, 2007, 6:33pm UTC](https://www.ruby-forum.com/t/preloading-a-controller-just-once/84252/1 "2007-02-02T18:33:00Z")

</div>

I’m looking for a way to have a controller pre-loaded at application  
startup and even in the development environment have it loaded just  
once.

I’ll make it a bit more concrete. I’m in the process of writing a little  
plugin for interrogating the state of a Rails app. Among other things,  
it ties into request handling and provides a hook for gathering  
statistics. The code for calculating a statistic I’d like to define  
within a controller. Currently it looks like this

class StatusController \< ApplicationController  
include HealthCheck

```
define_check :database_available do
  ActiveRecord::Base.connection != nil
end

define_statistic :request_count, :accumulator => [0] do |

```

accumulator, \*ignored|  
accumulator[0] += 1  
end  
end

With Rails working as it does, this code is only loaded when the status  
controller is accessed for the first time through a request. Only from  
then on are the defined statistics updated. In the development  
environment the accumulated values are forgotten again on the next  
request as classes are reloaded then.

To circumvent these issues, I’d like to load this particular controller  
on startup and don’t have it reloaded again. Any suggestions on how to  
achieve this?

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [February 2, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/preloading-a-controller-just-once/84252/2 "2007-02-02T18:40:01Z")

</div>

Hello Michael,

2007/2/2, Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> To circumvent these issues, I’d like to load this particular controller  
> on startup and don’t have it reloaded again. Any suggestions on how to  
> achieve this?

Did you try to require or reference StatusController in environment.rb  
? That would at least load the controller. Then, the reloading  
part…

[http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active\_support/dependencies.rb#L44](http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active_support/dependencies.rb#L44)

We have a way to unload constants, but not the reverse. Maybe this will  
help:  
#will\_unload?  
[http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active\_support/dependencies.rb#L288](http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active_support/dependencies.rb#L288)

## Hope that helps !

François Beausoleil  
[http://blog.teksol.info/](http://blog.teksol.info/)  
[http://piston.rubyforge.org/](http://piston.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [February 2, 2007, 10:26pm UTC](https://www.ruby-forum.com/t/preloading-a-controller-just-once/84252/3 "2007-02-02T22:26:27Z")

</div>

Hello Michael,

2007/2/2, Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> When I require StatusController in environment.rb, reloading is no  
> longer a problem. But as I said above, I don’t like that I have to  
> require ApplicationController in addition. This appears to be causing  
> other mayhem, too. Just randomly clicking around in the app gave me an  
> exception: ArgumentError “A copy of AuthenticatedSystem has been  
> removed from the module tree but is still active!”. I’m pretty sure  
> this is related to the explicit require for ApplicationController,  
> which includes AuthenticatedSystem.

Instead of requiring “application”, try to reference  
ApplicationController instead.

## Bye !

François Beausoleil  
[http://blog.teksol.info/](http://blog.teksol.info/)  
[http://piston.rubyforge.org/](http://piston.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [February 2, 2007, 8:59pm UTC](https://www.ruby-forum.com/t/preloading-a-controller-just-once/84252/4 "2007-02-02T20:59:18Z")

</div>

On Friday 02 February 2007 18:39, Francois B. wrote:

> Hello Michael,

Hi Francois, thanks for helping.

> 2007/2/2, Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > To circumvent these issues, I’d like to load this particular  
> > controller on startup and don’t have it reloaded again. Any  
> > suggestions on how to achieve this?
> 
> Did you try to require or reference StatusController in  
> environment.rb ? That would at least load the controller.

Yes, either results in an exception as the ApplicationController, the  
superclass of StatusController, is not yet loaded and can’t be  
autoloaded either, apparently. I can require ‘application’, of course,  
but that prevents reloading of ApplicationController even in the  
development environment, I gather.

> Then, the reloading part…

When I require StatusController in environment.rb, reloading is no  
longer a problem. But as I said above, I don’t like that I have to  
require ApplicationController in addition. This appears to be causing  
other mayhem, too. Just randomly clicking around in the app gave me an  
exception: ArgumentError “A copy of AuthenticatedSystem has been  
removed from the module tree but is still active!”. I’m pretty sure  
this is related to the explicit require for ApplicationController,  
which includes AuthenticatedSystem.

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [February 2, 2007, 11:17pm UTC](https://www.ruby-forum.com/t/preloading-a-controller-just-once/84252/5 "2007-02-02T23:17:33Z")

</div>

On Friday 02 February 2007 22:25, Francois B. wrote:

> > ApplicationController, which includes AuthenticatedSystem.
> 
> Instead of requiring “application”, try to reference  
> ApplicationController instead.

This immediately results in an exception: “uninitialized constant  
ApplicationController (NameError)”. I’ve tried it on a freshly  
generated project, too, and got the same exception.

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)
