How to call model that belongs to that controller

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

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

On Jul 20, 6:35 am, “Thriving K.” [email protected]

Thank you, i will try

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:

Enjoy!
Bryan

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

Enjoy!
Bryan

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

Models dont belong to a controller afaik

2009/7/20, Thriving K. [email protected]:

Posted via http://www.ruby-forum.com/.


Von meinen Mobilgerät aus gesendet

2009/7/20 Thriving K. [email protected]:

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