# How to call model that belongs to that controller

**URL:** <https://www.ruby-forum.com/t/how-to-call-model-that-belongs-to-that-controller/172355>\
**Category:** Rails\
**Created:** [July 20, 2009, 7:35am UTC](https://www.ruby-forum.com/t/how-to-call-model-that-belongs-to-that-controller/172355 "2009-07-20T07:35:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [July 20, 2009, 7:35am UTC](https://www.ruby-forum.com/t/how-to-call-model-that-belongs-to-that-controller/172355/1 "2009-07-20T07:35:40Z")

</div>

In controller ,there is something like self. to call the controller  
itsself

if i want to call model that belong to that controller in controller,  
How could i do.

Thank,  
Golf

---

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [July 20, 2009, 10:46am UTC](https://www.ruby-forum.com/t/how-to-call-model-that-belongs-to-that-controller/172355/2 "2009-07-20T10:46:03Z")

</div>

Hi

Models don’t belong to controllers. They are two separate classes  
that, to make things more practical, often have a similar name.

You could just as easily have a Post model and a BlogController and  
Rails wouldn’t know the difference.

If you’re trying to guess the name of model from the controller that  
deals with creating and maintaining its records you could try:

def get\_model  
self.class.to\_s.gsub(“Controller”,“”).singularize.constantize  
end

This would return Comment if called in the CommentsController.

Hope that helps?

Gavin

> **[Ruby on Rails tips & Tutorials](https://handyrailstips.com/)**
>
> Ruby on Rails tips and tutorials

On Jul 20, 6:35 am, “Thriving K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [July 20, 2009, 12:35pm UTC](https://www.ruby-forum.com/t/how-to-call-model-that-belongs-to-that-controller/172355/3 "2009-07-20T12:35:30Z")

</div>

Thank you, i will try

---

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [July 20, 2009, 3:17pm UTC](https://www.ruby-forum.com/t/how-to-call-model-that-belongs-to-that-controller/172355/4 "2009-07-20T15:17:15Z")

</div>

In Ruby there is always “something called self”. It’s really worth  
understanding what it is at any given time.

Dave T. gave a presentation on the Ruby Object Model at Scotland  
on Rails this year. The video is available:

[http://scotland-on-rails.s3.amazonaws.com/2A04\_DaveThomas-SOR.mp4](http://scotland-on-rails.s3.amazonaws.com/2A04_DaveThomas-SOR.mp4)

Enjoy!  
Bryan

---

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [July 21, 2009, 5:40am UTC](https://www.ruby-forum.com/t/how-to-call-model-that-belongs-to-that-controller/172355/5 "2009-07-21T05:40:45Z")

</div>

Bryan A. wrote:

> In Ruby there is always “something called self”. It’s really worth  
> understanding what it is at any given time.
> 
> Dave T. gave a presentation on the Ruby Object Model at Scotland  
> on Rails this year. The video is available:  
> [http://scotland-on-rails.s3.amazonaws.com/2A04\_DaveThomas-SOR.mp4](http://scotland-on-rails.s3.amazonaws.com/2A04_DaveThomas-SOR.mp4)
> 
> Enjoy!  
> Bryan

Thank you for Bryan too 🙂  
but it seem that link for the video is corrupted,or else it is problems  
of my internet connection ☹  
Does anyone has the similar video.

---

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [August 5, 2009, 7:22pm UTC](https://www.ruby-forum.com/t/how-to-call-model-that-belongs-to-that-controller/172355/6 "2009-08-05T19:22:56Z")

</div>

Models dont belong to a controller afaik

2009/7/20, Thriving K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Von meinen Mobilgerät aus gesendet

---

<div class="post-metadata">

**Author:** ![thriving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecae2f/32.png) [@thriving](https://www.ruby-forum.com/u/thriving)\
**Post date:** [July 21, 2009, 12:42pm UTC](https://www.ruby-forum.com/t/how-to-call-model-that-belongs-to-that-controller/172355/7 "2009-07-21T12:42:38Z")

</div>

2009/7/20 Thriving K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> In controller ,there is something like self. to call the controller  
> itsself
> 
> if i want to call model that belong to that controller in controller,  
> How could i do.

I am not sure what you are asking here, could you explain in a little  
more detail what you are trying to do? With an example if possible.

Colin
