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.
Nicholas H. (Guest)
on 2006-04-21 20:52
(Received via mailing list)
Is there a way to stop Rails automatically trying to load models?

Cheers,
Nicholas
Alex W. (Guest)
on 2006-04-21 21:14
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
Nicholas H. (Guest)
on 2006-04-21 21: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
Kevin O. (Guest)
on 2006-04-21 21: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 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
>>
>_______________________________________________
>Rails mailing list
>removed_email_address@domain.invalid
>http://lists.rubyonrails.org/mailman/listinfo/rails


_Kevin
Alex W. (Guest)
on 2006-04-21 21:40
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 :)
Nicholas H. (Guest)
on 2006-04-21 21: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 O.
Nicholas H. (Guest)
on 2006-04-21 21:54
(Received via mailing list)
Thanks, Alex, unfortunately Rails still trys to require/load even if
the Class/Model is already avaliable.
Trevor S. (Guest)
on 2006-04-22 00: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 S.
http://somethinglearned.com
Julian 'Julik' Tarkhanov (Guest)
on 2006-04-22 20:30
(Received via mailing list)
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
This topic is locked and can not be replied to.