When is model() needed?


#1

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


#2

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


#3

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


#4

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.


#5

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


#6

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