# Stop Models auto loading

**URL:** <https://www.ruby-forum.com/t/stop-models-auto-loading/94118>\
**Category:** Rails\
**Created:** [April 21, 2006, 6:52pm UTC](https://www.ruby-forum.com/t/stop-models-auto-loading/94118 "2006-04-21T18:52:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicholas-henry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bcef8e/32.png) [@nicholas-henry](https://www.ruby-forum.com/u/nicholas-henry)\
**Post date:** [April 21, 2006, 6:52pm UTC](https://www.ruby-forum.com/t/stop-models-auto-loading/94118/1 "2006-04-21T18:52:21Z")

</div>

Is there a way to stop Rails automatically trying to load models?

Cheers,  
Nicholas

---

<div class="post-metadata">

**Author:** ![nicholas-henry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bcef8e/32.png) [@nicholas-henry](https://www.ruby-forum.com/u/nicholas-henry)\
**Post date:** [April 21, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/stop-models-auto-loading/94118/2 "2006-04-21T19:14:19Z")

</div>

By loading, do you mean accessing the model’s class on boot up?  
Retrieving database rows?

Nicholas H. wrote:

> Is there a way to stop Rails automatically trying to load models?
> 
> Cheers,  
> Nicholas

---

<div class="post-metadata">

**Author:** ![nicholas-henry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bcef8e/32.png) [@nicholas-henry](https://www.ruby-forum.com/u/nicholas-henry)\
**Post date:** [April 21, 2006, 7:23pm UTC](https://www.ruby-forum.com/t/stop-models-auto-loading/94118/3 "2006-04-21T19:23:12Z")

</div>

Alex,

I want to stop the automatically requiring/loading of models when I am  
calling it from a controller e.g.

No such file to load – user.rb

as my actual models are outside of the model directory. (it’s a long  
complicated story 😉 )

I don’t want rails to do this at all, as I want to have have the  
control of doing the require.

Cheers,  
Nicholas

---

<div class="post-metadata">

**Author:** ![nicholas-henry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bcef8e/32.png) [@nicholas-henry](https://www.ruby-forum.com/u/nicholas-henry)\
**Post date:** [April 21, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/stop-models-auto-loading/94118/4 "2006-04-21T19:35:31Z")

</div>

I don’t know how to disable it, but you might want to take a look at the  
‘Sharing Models between Applications’ recipe in Chad F.'s book.

Most of the methods mentioned either make copies of the model files in  
the right place, or tell the OS to look for them elsewhere.

On Friday, April 21, 2006, at 1:20 PM, Nicholas H. wrote:

> I don’t want rails to do this at all, as I want to have have the
> 
> > > Is there a way to stop Rails automatically trying to load models?  
> > > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

\_Kevin

---

<div class="post-metadata">

**Author:** ![nicholas-henry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bcef8e/32.png) [@nicholas-henry](https://www.ruby-forum.com/u/nicholas-henry)\
**Post date:** [April 21, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/stop-models-auto-loading/94118/5 "2006-04-21T19:41:43Z")

</div>

Thanks Kevin:

In this case it’s a matter of not sharing models, but having control  
of what models are required/loaded.

Cheers,  
Nicholas

On 21 Apr 2006 17:33:33 -0000, Kevin O.

---

<div class="post-metadata">

**Author:** ![nicholas-henry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bcef8e/32.png) [@nicholas-henry](https://www.ruby-forum.com/u/nicholas-henry)\
**Post date:** [April 21, 2006, 7:40pm UTC](https://www.ruby-forum.com/t/stop-models-auto-loading/94118/6 "2006-04-21T19:40:24Z")

</div>

Nicholas H. wrote:

> Alex,
> 
> I want to stop the automatically requiring/loading of models when I am  
> calling it from a controller e.g.
> 
> No such file to load – user.rb
> 
> as my actual models are outside of the model directory. (it’s a long  
> complicated story 😉 )
> 
> I don’t want rails to do this at all, as I want to have have the  
> control of doing the require.
> 
> Cheers,  
> Nicholas

You might try requiring the proper file in application.rb.

class ApplicationController \< ActionController::Base  
reuire ‘path/to/my\_models/user’  
end

That way class User is loaded and available, without looking the model  
directory. The question is, is Rails smart enough to not load a model  
if defined?(User)

I’m thinking that have a class of User already should prevent Rails from  
loading a file to find it. Totally untested solution but there is a  
chance it might work 🙂

---

<div class="post-metadata">

**Author:** ![nicholas-henry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bcef8e/32.png) [@nicholas-henry](https://www.ruby-forum.com/u/nicholas-henry)\
**Post date:** [April 21, 2006, 7:54pm UTC](https://www.ruby-forum.com/t/stop-models-auto-loading/94118/7 "2006-04-21T19:54:05Z")

</div>

Thanks, Alex, unfortunately Rails still trys to require/load even if  
the Class/Model is already avaliable.

---

<div class="post-metadata">

**Author:** ![nicholas-henry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bcef8e/32.png) [@nicholas-henry](https://www.ruby-forum.com/u/nicholas-henry)\
**Post date:** [April 22, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/stop-models-auto-loading/94118/8 "2006-04-22T18:30:44Z")

</div>

On 21-apr-2006, at 22:35, Trevor S. wrote:

> Hi Nicholas,
> 
> your statement that rails “still tries to require/load even if the  
> Class/Model is already available” doesn’t make sense.
> 
> Rails only auto-loads your models as part of a const\_missing hook -  
> which means that from ruby’s perspective, the constant you are  
> trying to use doesn’t (yet) exist.
> 
> You can try this for yourself:

Rails also autoloads the models inferred from the associations. I  
guess this is the culprit here.

–  
Julian ‘Julik’ Tarkhanov  
please send all personal mail to  
me at julik.nl

---

<div class="post-metadata">

**Author:** ![nicholas-henry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/bcef8e/32.png) [@nicholas-henry](https://www.ruby-forum.com/u/nicholas-henry)\
**Post date:** [April 21, 2006, 10:40pm UTC](https://www.ruby-forum.com/t/stop-models-auto-loading/94118/9 "2006-04-21T22:40:07Z")

</div>

Hi Nicholas,

your statement that rails “still tries to require/load even if the  
Class/Model is already available” doesn’t make sense.

Rails only auto-loads your models as part of a const\_missing hook -  
which means that from ruby’s perspective, the constant you are trying  
to use doesn’t (yet) exist.

You can try this for yourself:

in models/wibble.rb

class Wibble  
puts “#{self.name} loaded”  
end

Then run ./script console and type:

Wibble

it should print out “Wibble loaded”

quit from the console and run ./script/console again, typing this:

class Wibble  
end  
Wibble

it should _not_ print out “Wibble loaded”

As in, Wibble has already been defined so there’s no need to go  
looking for (and load) a wibble.rb file…

You might need to post more complete information to diagnose what’s  
actually going on.

## HTH Trevor

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)
