Accessing the Model class from Controller

Hi,

I wonder if there’s an easy way to access the associated Model class
from its Controller. Something that would allow me to write snippets
like the following:

class PeopleController < ApplicationController
def some_method
person_model_class =
self.some_method_to_retrieve_the_person_model_class()
end
end

Cheers,
Marco

Marco L. wrote:

end

end

Cheers,
Marco

Hello,

Had the same problem here… Controllers aren’t associated to one
specific model. But yes, for general CRUD operations, you’ll usually
just use one… So, this will probably do what you want:

model = Object.const_get controller_name.camelcase

The benefit is that you can have PeopleController < GenericController,
and define generic CRUD things in the latter.

Have a lot of fun,

Yannick M. http://www.inma.ucl.ac.be/~majoros
Informaticien UCL/INMA-MEMA
4, avenue G. Lemaître
B-1348 Louvain-la-Neuve
Tel: +32-10-47.80.10
Fax: +32-10-47.21.80

Although common practice to create one controller per model, there’s
nothing to say there should be a one-to-one or indeed any relationship
between models and controllers. One controller may deal with many
models, one model may be used in many controllers.

I’m guessing you want to do this to reuse code in some way. If so,
perhaps if you explain which code you want to reuse the list can help
come up with an appropriate strategy (abstract class, module, etc).

Tom

On 7/14/06, Yannick M. [email protected] wrote:

So, this will probably do what you want:

model = Object.const_get controller_name.camelcase

You’ll probably need to singularize the name as well:

model = Object.const_get controller_name.singularize.camelcase

Tom