# When is model() needed?

**URL:** <https://www.ruby-forum.com/t/when-is-model-needed/56875>\
**Category:** Rails\
**Created:** [April 5, 2006, 10:15am UTC](https://www.ruby-forum.com/t/when-is-model-needed/56875 "2006-04-05T10:15:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [April 5, 2006, 10:15am UTC](https://www.ruby-forum.com/t/when-is-model-needed/56875/1 "2006-04-05T10:15:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [April 5, 2006, 10:36am UTC](https://www.ruby-forum.com/t/when-is-model-needed/56875/2 "2006-04-05T10:36:05Z")

</div>

I’m interested too - I didn’t found any doc about model (nothing here  
[ActionController::Base](http://api.rubyonrails.org/classes/ActionController/Base.html), for  
example)

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [April 5, 2006, 11:41am UTC](https://www.ruby-forum.com/t/when-is-model-needed/56875/3 "2006-04-05T11:41:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [April 5, 2006, 11:22pm UTC](https://www.ruby-forum.com/t/when-is-model-needed/56875/4 "2006-04-05T23:22:12Z")

</div>

On 4/5/06, Xavier N. [removed\_email\_address@domain.invalid](mailto: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.

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [April 5, 2006, 11:20am UTC](https://www.ruby-forum.com/t/when-is-model-needed/56875/5 "2006-04-05T11:20:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [April 5, 2006, 11:41pm UTC](https://www.ruby-forum.com/t/when-is-model-needed/56875/6 "2006-04-05T23:41:34Z")

</div>

Wilson B. wrote:

> On 4/5/06, Xavier N. [removed\_email\_address@domain.invalid](mailto: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
