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

http://handyrailstips.com

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

Dave Thomas 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

Bryan Ash wrote:

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

Dave Thomas 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.

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

Models dont belong to a controller afaik