Forum: Ruby on Rails when is model() needed?

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.
Xavier N. (Guest)
on 2006-04-05 12:15
(Received via mailing list)
I have just some vague and thus probably wrong ideas about when to
use model() in controllers, and by now I err on being redundant. I
don't post them here to avoid leaving anything that may be false in
the archives.

Can anyone explain exactly in which cases one should use model()?

-- fxn
Nuno (Guest)
on 2006-04-05 12:36
I'm interested too - I didn't found any doc about model (nothing here
http://api.rubyonrails.org/classes/ActionControlle..., for
example)
Chris T (Guest)
on 2006-04-05 13:20
(Received via mailing list)
According to my well-thumbed copy of AWDWR, the model declaration
preloads the model so that the app has access to the methods (and
subclasses) within that model. The examples it gives are when your
classes are stored in session (so they can be deserialized correctly),
when you have a controller that uses a number of different models (and
thus you can preload them all, together with their helpers -- or I
believe you can user helper instead of model just to load the helpers).

I also use model in application.rb to declare a class that contains
subclasses through STI (this avoids having to have separate files for
each subclass).

I've got a very vague feeling that prior to rails 0.13 you might have
had to use model to decalre models explicitly, but don't quote me on
that -- I'ver only been using rails since 1.0.

Hope this helps
Chris T
Xavier N. (Guest)
on 2006-04-05 13:41
(Received via mailing list)
On Apr 5, 2006, at 11:16, Chris T wrote:

> subclasses through STI (this avoids having to have separate files
> for each subclass).
>
> I've got a very vague feeling that prior to rails 0.13 you might
> have had to use model to decalre models explicitly, but don't quote
> me on that -- I'ver only been using rails since 1.0.

Yes, yes, I have read AWDWR.

But that's part of my vague ideas, I would like to have a clear and
precise explanation of when you need to use model(), looks like that
is missing in the API.

-- fxn
Wilson B. (Guest)
on 2006-04-06 01:22
(Received via mailing list)
On 4/5/06, Xavier N. <removed_email_address@domain.invalid> wrote:
> >
> But that's part of my vague ideas, I would like to have a clear and
> precise explanation of when you need to use model(), looks like that
> is missing in the API.
>

Given the enhancements to "const_missing", the answer is pretty much
never.
The only reason I can think of for using it now is if you have model
classes with names that can't be inferred from their filenames.
(e.g. Example < ActiveRecord::Base inside a file called not_example.rb)
Of course, the right answer is "don't do that".. but if you did for
some reason, you'd need to declare them with the 'model()' method.

--Wilson.
Jeff C. (Guest)
on 2006-04-06 01:41
Wilson B. wrote:
> On 4/5/06, Xavier N. <removed_email_address@domain.invalid> wrote:
>> >
>> But that's part of my vague ideas, I would like to have a clear and
>> precise explanation of when you need to use model(), looks like that
>> is missing in the API.
>>
>
> Given the enhancements to "const_missing", the answer is pretty much
> never.
> The only reason I can think of for using it now is if you have model
> classes with names that can't be inferred from their filenames.
> (e.g. Example < ActiveRecord::Base inside a file called not_example.rb)
> Of course, the right answer is "don't do that".. but if you did for
> some reason, you'd need to declare them with the 'model()' method.
>
> --Wilson.

I wanted to create a paginator object on a different model than the
current controller.  I have a house_controller and a House model, and
wanted to paginate records from my Image model.  Once I added "model
:image" to my house_controller, it worked as expected.

Jeff
This topic is locked and can not be replied to.