# Obtaining a model name

**URL:** <https://www.ruby-forum.com/t/obtaining-a-model-name/115577>\
**Category:** Rails\
**Created:** [September 26, 2007, 2:47pm UTC](https://www.ruby-forum.com/t/obtaining-a-model-name/115577 "2007-09-26T14:47:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![whitewolfaustria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bbce88/32.png) [@whitewolfaustria](https://www.ruby-forum.com/u/whitewolfaustria)\
**Post date:** [September 26, 2007, 2:47pm UTC](https://www.ruby-forum.com/t/obtaining-a-model-name/115577/1 "2007-09-26T14:47:40Z")

</div>

Dear user community,

I know that I can access the controller that is called by using  
params[:controller] . I need to obtain the model name for a find  
command and I was wondering if there was a built in way of obtaining  
this name. If I can’t obtain it I have to ways to obtain it, by using  
the controller and modding the string as I go through it or creating  
a database and find the association in the database.

I would much rather use a built in variable or method to obtain this  
information.

Any and all help is appreciated

thanks

---

<div class="post-metadata">

**Author:** ![whitewolfaustria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bbce88/32.png) [@whitewolfaustria](https://www.ruby-forum.com/u/whitewolfaustria)\
**Post date:** [September 26, 2007, 3:51pm UTC](https://www.ruby-forum.com/t/obtaining-a-model-name/115577/2 "2007-09-26T15:51:43Z")

</div>

What exactly do you want to do with the model ‘name’. I believe you  
already know your model class name. And if you are talking about  
getting reference of the model object in your controller then you have  
the option of creating sessions or using a global hash.

Please throw in more details about your requirement.

Thanks,  
~Shishir

---

<div class="post-metadata">

**Author:** ![whitewolfaustria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bbce88/32.png) [@whitewolfaustria](https://www.ruby-forum.com/u/whitewolfaustria)\
**Post date:** [September 26, 2007, 5:09pm UTC](https://www.ruby-forum.com/t/obtaining-a-model-name/115577/3 "2007-09-26T17:09:48Z")

</div>

Is it inside of a model? If so, you could likely do something like…

self.class.find(:first)

…or whatever find command you’re looking for.

If that’s not what you need, then we need more details, please. 🙂

–Jeremy

On 9/26/07, White W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I would much rather use a built in variable or method to obtain this  
> information.
> 
> Any and all help is appreciated
> 
> thanks
> 
> >

–  
[http://www.jeremymcanally.com/](http://www.jeremymcanally.com/)

My free Ruby e-book:  
[http://www.humblelittlerubybook.com/book/](http://www.humblelittlerubybook.com/book/)

My blogs:

> **[Site not found · DreamHost](http://www.mrneighborly.com/)**
>
> The owner of this domain has not yet uploaded their website.

[http://www.rubyinpractice.com/](http://www.rubyinpractice.com/)

---

<div class="post-metadata">

**Author:** ![whitewolfaustria](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bbce88/32.png) [@whitewolfaustria](https://www.ruby-forum.com/u/whitewolfaustria)\
**Post date:** [September 27, 2007, 5:52pm UTC](https://www.ruby-forum.com/t/obtaining-a-model-name/115577/4 "2007-09-27T17:52:01Z")

</div>

You can do something like this.

case1: Array of object returned by your query like this  
x = Product.find(:all) # may what ever query you want.  
model\_name = x.first.class  
puts model\_name # outputs Product  
case2: Your query returns only single object  
x = Product.find(:first) # may what ever query you want.  
model\_name = x.class  
puts model\_name # outputs Product
