# Class loading in development mode best practice / recommendation

**URL:** <https://www.ruby-forum.com/t/class-loading-in-development-mode-best-practice-recommendation/191186>\
**Category:** Rails\
**Created:** [July 7, 2010, 10:03am UTC](https://www.ruby-forum.com/t/class-loading-in-development-mode-best-practice-recommendation/191186 "2010-07-07T10:03:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [July 7, 2010, 10:03am UTC](https://www.ruby-forum.com/t/class-loading-in-development-mode-best-practice-recommendation/191186/1 "2010-07-07T10:03:32Z")

</div>

In my project, I have a user model. The user model can be assigned  
chores. Chores have an associated class object (that go by the name  
‘Service’). In the admin interface for users, the administrator can  
assign these Chores to a user, and when they do this, they select the  
Service for the Chore from a dropdown. My problem is getting the  
service list.

All service classes inherit from Services::Base (MyService \<  
Services::Base) I want to provide the list of available services in  
that dropdown, but when in development mode, the classes are unloaded  
and reloaded with each request. Thus, using  
Object.subclasses\_of( Services::Base ) will not reliably get me a list  
of available services, as the classes have probably been unloaded by  
the time the code is reached.

Alternatively, I wanted to register available services via an  
initializer, like: add\_service MyService, but this depends on storing  
values in a class, which also fails, for the same reason, as far as I  
can tell (because the classes are being unloaded after each request).

Note, both of these methods work fine in test and production mode.

If there is any other information I can provide, please let me know.  
Thanks all for the help.

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [July 7, 2010, 10:26am UTC](https://www.ruby-forum.com/t/class-loading-in-development-mode-best-practice-recommendation/191186/2 "2010-07-07T10:26:22Z")

</div>

On Jul 6, 7:27 pm, Joe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> and reloaded with each request. Thus, using  
> Object.subclasses\_of( Services::Base ) will not reliably get me a list  
> of available services, as the classes have probably been unloaded by  
> the time the code is reached.

I usually stick a bunch of require\_dependency calls at the bottom of  
the file containing the base class that loads the various subclasses

Fred

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [July 7, 2010, 4:53pm UTC](https://www.ruby-forum.com/t/class-loading-in-development-mode-best-practice-recommendation/191186/3 "2010-07-07T16:53:29Z")

</div>

On Jul 7, 3:25 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I usually stick a bunch of require\_dependency calls at the bottom of  
> the file containing the base class that loads the various subclasses
> 
> Fred

Thanks for the suggestion, Fred. That makes sense, and I would solve  
the problem in this case.

Long term, I’m thinking of moving the framework out of the project and  
into a gem. At that point, of course, it would be unrealistic to  
include the require\_dependency lines. Is there some other place you  
can think of that I might put the code so that it’s called with each  
request, but not a part of the gem itself?

Thanks,  
Joe

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [July 8, 2010, 9:13am UTC](https://www.ruby-forum.com/t/class-loading-in-development-mode-best-practice-recommendation/191186/4 "2010-07-08T09:13:21Z")

</div>

On Jul 7, 3:52 pm, Joe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for the suggestion, Fred. That makes sense, and I would solve  
> the problem in this case.
> 
> Long term, I’m thinking of moving the framework out of the project and  
> into a gem. At that point, of course, it would be unrealistic to  
> include the require\_dependency lines. Is there some other place you  
> can think of that I might put the code so that it’s called with each  
> request, but not a part of the gem itself?

If you set config.to\_prepare to a block then it’s called before each  
request (in development mode). Also code from gems isn’t reloaded  
between requests

Fred
