# Call Model Method

**URL:** <https://www.ruby-forum.com/t/call-model-method/159398>\
**Category:** Rails\
**Created:** [February 4, 2009, 7:36am UTC](https://www.ruby-forum.com/t/call-model-method/159398 "2009-02-04T07:36:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [February 4, 2009, 7:36am UTC](https://www.ruby-forum.com/t/call-model-method/159398/1 "2009-02-04T07:36:17Z")

</div>

Hello Everyone

any one know how to call model method in controller ?

I have one method that is in model and want to call from another  
controller

please help me

thanks

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [February 4, 2009, 7:51am UTC](https://www.ruby-forum.com/t/call-model-method/159398/2 "2009-02-04T07:51:34Z")

</div>

Is it a class method or an instance method? Meaning do you call it from  
an instance of that model:

@model.some\_method

Or do you call it on the model itself:

Model.some\_method?

If it’s an instance method, you just need to have an instance in the  
controller action:

def index  
@instance = Model.find(params[:id])  
@instance.some\_method  
end

If you’re dealing with a class method you would do this:

def index  
Model.some\_method  
end

Note: swap out “Model” for the actual name of the model (User, BlogPost,  
etc.)

Hope this helps. Post if you have any other questions.

– Josh  
[http://iammrjoshua.com](http://iammrjoshua.com)

Cyrus D. wrote:

> Hello Everyone
> 
> any one know how to call model method in controller ?
> 
> I have one method that is in model and want to call from another  
> controller
> 
> please help me
> 
> thanks

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [February 4, 2009, 9:32am UTC](https://www.ruby-forum.com/t/call-model-method/159398/3 "2009-02-04T09:32:28Z")

</div>

Model.method

Controllers and models are not related so you can do this from any  
controller

On 04/02/2009, at 16:36, Cyrus D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@cyrus](https://www.ruby-forum.com/u/cyrus)\
**Post date:** [March 19, 2009, 6:26pm UTC](https://www.ruby-forum.com/t/call-model-method/159398/4 "2009-03-19T18:26:52Z")

</div>

If you make sure that method is defined with “self”, you can call it  
from the controllers. For example,

#A Method that can be called from a controller.  
def self.my\_hello\_string  
“Hello!”  
end

Now you can say Model.my\_hello\_string from a controller, and it will  
return what you expect, instead of complaining that the method isn’t  
defined.
