# Accessing the Model class from Controller

**URL:** <https://www.ruby-forum.com/t/accessing-the-model-class-from-controller/98412>\
**Category:** Rails\
**Created:** [July 14, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/accessing-the-model-class-from-controller/98412 "2006-07-14T16:27:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mclow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d6d6ee/32.png) [@mclow](https://www.ruby-forum.com/u/mclow)\
**Post date:** [July 14, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/accessing-the-model-class-from-controller/98412/1 "2006-07-14T16:27:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mclow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d6d6ee/32.png) [@mclow](https://www.ruby-forum.com/u/mclow)\
**Post date:** [July 14, 2006, 4:53pm UTC](https://www.ruby-forum.com/t/accessing-the-model-class-from-controller/98412/2 "2006-07-14T16:53:15Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![mclow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d6d6ee/32.png) [@mclow](https://www.ruby-forum.com/u/mclow)\
**Post date:** [July 14, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/accessing-the-model-class-from-controller/98412/3 "2006-07-14T17:00:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mclow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d6d6ee/32.png) [@mclow](https://www.ruby-forum.com/u/mclow)\
**Post date:** [July 14, 2006, 5:03pm UTC](https://www.ruby-forum.com/t/accessing-the-model-class-from-controller/98412/4 "2006-07-14T17:03:36Z")

</div>

On 7/14/06, Yannick M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
