Forum: Ruby on Rails Stop Models auto loading

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
833fbd3f58a8ebbeb43756e8634334ed?d=identicon&s=25 Nicholas Henry (Guest)
on 2006-04-21 18:52
(Received via mailing list)
Is there a way to stop Rails automatically trying to load models?

Cheers,
Nicholas
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-04-21 19:14
By loading, do you mean accessing the model's class on boot up?
Retrieving database rows?

Nicholas Henry wrote:
> Is there a way to stop Rails automatically trying to load models?
>
> Cheers,
> Nicholas
833fbd3f58a8ebbeb43756e8634334ed?d=identicon&s=25 Nicholas Henry (Guest)
on 2006-04-21 19:23
(Received via mailing list)
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
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-04-21 19:35
(Received via mailing list)
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 Fowler'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 Henry 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
>>
>_______________________________________________
>Rails mailing list
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails


_Kevin
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-04-21 19:40
Nicholas Henry 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 :)
833fbd3f58a8ebbeb43756e8634334ed?d=identicon&s=25 Nicholas Henry (Guest)
on 2006-04-21 19:41
(Received via mailing list)
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 Olbrich
833fbd3f58a8ebbeb43756e8634334ed?d=identicon&s=25 Nicholas Henry (Guest)
on 2006-04-21 19:54
(Received via mailing list)
Thanks, Alex, unfortunately Rails still trys to require/load even if
the Class/Model is already avaliable.
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2006-04-21 22:40
(Received via mailing list)
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 Squires
http://somethinglearned.com
A2b2f4ee23989dc68529baef9cbddcd6?d=identicon&s=25 Julian 'Julik' Tarkhanov (Guest)
on 2006-04-22 18:30
(Received via mailing list)
On 21-apr-2006, at 22:35, Trevor Squires 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
This topic is locked and can not be replied to.