# Preload class at start up

**URL:** <https://www.ruby-forum.com/t/preload-class-at-start-up/81766>\
**Category:** Rails\
**Created:** [January 13, 2007, 4:26am UTC](https://www.ruby-forum.com/t/preload-class-at-start-up/81766 "2007-01-13T04:26:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinhtiensinh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vinhtiensinh](https://www.ruby-forum.com/u/vinhtiensinh)\
**Post date:** [January 13, 2007, 4:26am UTC](https://www.ruby-forum.com/t/preload-class-at-start-up/81766/1 "2007-01-13T04:26:04Z")

</div>

Hi All,

Is there a way to tell Rail to load my class at start up. my problem is

I have a Field class which has children classes : DateField, MoneyField  
…etc (and might be more in the future)

I also have Template class which can have many Fields  
I want to display a list of available Fields type when I create new  
Template.

What I was trying to do, and doesnt work :(, is using call back  
Class$inherited on the Field Class someting like

@@field\_type\_list = []

def Field.inherited(sub)  
@@field\_type\_list.push({‘name’=\>sub.display\_name,‘type’=\>sub})  
end  
def Field.display\_name  
raise NotImplementedError("#{self.class.name}#m1() is not  
implemented.")  
end

```
    def Field.field_type_list
return @@field_type_list

```

## end

and from my helper I call Field.field\_type\_list to get all the  
subclasses  
However Rail only load a class when it needed, so it left my  
field\_type\_list empty

so I need a way to load my sub classes at start up make sure they’re  
available to be used.

Can anyone help please, or suggest better design for this

Thanks

---

<div class="post-metadata">

**Author:** ![vinhtiensinh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vinhtiensinh](https://www.ruby-forum.com/u/vinhtiensinh)\
**Post date:** [January 13, 2007, 5:36am UTC](https://www.ruby-forum.com/t/preload-class-at-start-up/81766/2 "2007-01-13T05:36:08Z")

</div>

You could try putting the following lines at the _bottom_ of field.rb:

require ‘date\_field’  
require ‘money\_field’  
… etc.

I’m not sure whether this will work properly with the Rails “reloading”  
feature, but it’s worth a try.

---

<div class="post-metadata">

**Author:** ![vinhtiensinh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vinhtiensinh](https://www.ruby-forum.com/u/vinhtiensinh)\
**Post date:** [January 14, 2007, 2:09am UTC](https://www.ruby-forum.com/t/preload-class-at-start-up/81766/3 "2007-01-14T02:09:45Z")

</div>

Thanks  
But that way, I’ll need to know what Field Type I’m dealing with. I want  
to be able to let others define their own Field Type later on. pretty  
much like a plugin…

I wonder if there’s a way to do that, but in the meantime , I think I’m  
gonna go with this

Thank you

---

<div class="post-metadata">

**Author:** ![vinhtiensinh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vinhtiensinh](https://www.ruby-forum.com/u/vinhtiensinh)\
**Post date:** [January 14, 2007, 2:15am UTC](https://www.ruby-forum.com/t/preload-class-at-start-up/81766/4 "2007-01-14T02:15:36Z")

</div>

Oops…  
It doesnt work, because it required a child class which inherited from  
itself …  
it gave me a uninitilized constant Field error

---

<div class="post-metadata">

**Author:** ![vinhtiensinh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vinhtiensinh](https://www.ruby-forum.com/u/vinhtiensinh)\
**Post date:** [January 14, 2007, 2:33am UTC](https://www.ruby-forum.com/t/preload-class-at-start-up/81766/5 "2007-01-14T02:33:13Z")

</div>

Did you put the “require” lines at the bottom of the file like I  
suggested, or at the top? Obviously you have to require the derived  
classes AFTER you’ve defined the base class. If you already did this  
then Ruby might be choking on the circular dependencies between the  
files, but I just did a quick test and it seems to work ok for me.

Another option would be to put the “requires” in environment.rb (with  
instructions for other users of your plugin to do the same). Or you  
could put your requires in field.rb but instruct your users to put  
additional requires in a location of their choice, like environment.rb.

Or you could even maintain a list of which field types are available in  
the database.

---

<div class="post-metadata">

**Author:** ![vinhtiensinh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b38774/32.png) [@vinhtiensinh](https://www.ruby-forum.com/u/vinhtiensinh)\
**Post date:** [January 14, 2007, 12:47pm UTC](https://www.ruby-forum.com/t/preload-class-at-start-up/81766/6 "2007-01-14T12:47:17Z")

</div>

Snow Man wrote:

> Did you put the “require” lines at the bottom of the file like I  
> suggested, or at the top? Obviously you have to require the derived  
> classes AFTER you’ve defined the base class. If you already did this  
> then Ruby might be choking on the circular dependencies between the  
> files, but I just did a quick test and it seems to work ok for me.
> 
> Another option would be to put the “requires” in environment.rb (with  
> instructions for other users of your plugin to do the same). Or you  
> could put your requires in field.rb but instruct your users to put  
> additional requires in a location of their choice, like environment.rb.
> 
> Or you could even maintain a list of which field types are available in  
> the database.

yeah, I put it at the bottom, not really sure what happened. I ended up  
put in in application.rb (controller) and its fine now.  
still think that I would need to refactor this later 😃

Thanks for your help
