# How to call method declared in one model

**URL:** <https://discuss.rubyonrails.org/t/how-to-call-method-declared-in-one-model/45634>\
**Category:** rubyonrails-talk\
**Created:** [August 31, 2009, 7:52am UTC](https://discuss.rubyonrails.org/t/how-to-call-method-declared-in-one-model/45634 "2009-08-31T07:52:18Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![karthik\_k](https://avatars.discourse-cdn.com/v4/letter/k/f0a364/32.png) [@karthik\_k](https://discuss.rubyonrails.org/u/karthik_k)\
**Post date:** [August 31, 2009, 7:52am UTC](https://discuss.rubyonrails.org/t/how-to-call-method-declared-in-one-model/45634/1 "2009-08-31T07:52:18Z")

</div>

hi guys

I need to know how to call one method declared in one model to another controller or another model

e.g

model1.rb

def check1 puts “check1” end

model2.rb

def check2 puts “check2” end

i need to access check2 method from check1 in model1.rb my line to access check2 method is

model2.check2

but i am not getting this

should i need to do any thing plz guide me

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [August 31, 2009, 8:34am UTC](https://discuss.rubyonrails.org/t/how-to-call-method-declared-in-one-model/45634/2 "2009-08-31T08:34:07Z")

</div>

I believe that You cannot call a controller model from another controller in this way. If you need to do this it probably means that the method should be in a model or in a module in the lib directory. Or are you actually trying to redirect to a different action rather than just call a method?

Colin

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [August 31, 2009, 10:03am UTC](https://discuss.rubyonrails.org/t/how-to-call-method-declared-in-one-model/45634/3 "2009-08-31T10:03:38Z")

</div>

Sorry I misread the question and the reply above is rubbish anyway. I thought you were wanting to call a controller method from another controller. I must remember to engage brain before typing.

Colin

---

<div class="post-metadata">

**Author:** ![karthik\_k](https://avatars.discourse-cdn.com/v4/letter/k/f0a364/32.png) [@karthik\_k](https://discuss.rubyonrails.org/u/karthik_k)\
**Post date:** [August 31, 2009, 10:08am UTC](https://discuss.rubyonrails.org/t/how-to-call-method-declared-in-one-model/45634/4 "2009-08-31T10:08:01Z")

</div>

hi Colin

That is not a problem

You have the mind to help that is far more enough even if you misjudged

Thank you guys

You save me

---

<div class="post-metadata">

**Author:** ![Abhinav\_Saxena](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Abhinav\_Saxena](https://discuss.rubyonrails.org/u/Abhinav_Saxena)\
**Post date:** [August 31, 2009, 10:23am UTC](https://discuss.rubyonrails.org/t/how-to-call-method-declared-in-one-model/45634/5 "2009-08-31T10:23:40Z")

</div>

If you want to call check1 from another model/controller then you can do any of the following:

=\> make check1 class method

## model1.rb

def self.check1 end

# model2.rb

def check2.rb Model1.check1 end

=\> or if you want to keep it as a instance method (or it depends on the state of object)

## model1.rb

def check1 end

## model2.rb

def check2.rb @model1.check1 end

---

<div class="post-metadata">

**Author:** ![karthik\_k](https://avatars.discourse-cdn.com/v4/letter/k/f0a364/32.png) [@karthik\_k](https://discuss.rubyonrails.org/u/karthik_k)\
**Post date:** [August 31, 2009, 10:25am UTC](https://discuss.rubyonrails.org/t/how-to-call-method-declared-in-one-model/45634/6 "2009-08-31T10:25:46Z")

</div>

Hi Guys

This is anew Question

i am writing a method in model

In check1.rb //model def check? [self.name](http://self.name)=“karthik”

end

when i call from controller say check2controller.rb //different controller

i am calling as check1=check1.new check1.check? //returns true or false

i need to pass karthik as paramater to check?in check1.rb

so that it will return true or false

Please guide me how to do

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [August 31, 2009, 10:34am UTC](https://discuss.rubyonrails.org/t/how-to-call-method-declared-in-one-model/45634/7 "2009-08-31T10:34:19Z")

</div>

> This is anew Question
> 
> i am writing a method in model
> 
> In check1.rb //model def check? self.name="karthik"

I think that should be self.name == "Karthik"

> end
> 
> when i call from controller say check2controller.rb //different controller
> 
> i am calling as check1=check1.new check1.check? //returns true or false
> 
> i need to pass karthik as paramater to check?in check1.rb so that it will return true or false
> 
> Please guide me how to do --

def check?( a\_name ) &nbsp;&nbsp;self.name == a\_name end

Then check1.check?( "karthik" )

If you call it immediately after check1.new then the name will always be the default one for the name attribute of course. check1.check?( "karthik" ) means hey object check1, is your name karthik?

Colin

---

<div class="post-metadata">

**Author:** ![Abhinav\_Saxena](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Abhinav\_Saxena](https://discuss.rubyonrails.org/u/Abhinav_Saxena)\
**Post date:** [August 31, 2009, 10:35am UTC](https://discuss.rubyonrails.org/t/how-to-call-method-declared-in-one-model/45634/8 "2009-08-31T10:35:53Z")

</div>

Hi Karthik,

There are a lot of mistakes (bug) in your code, but I think philosophically too you are wrong.

Firstly, you have “=” instead of “==” in your check? method. And if the word “karthik” is constant you don’t need to pass it. I think this solves your problem.

But please do read some detailed tutorial or preferably a book on rails, because I think you are missing too many points here.

---

<div class="post-metadata">

**Author:** ![karthik\_k](https://avatars.discourse-cdn.com/v4/letter/k/f0a364/32.png) [@karthik\_k](https://discuss.rubyonrails.org/u/karthik_k)\
**Post date:** [August 31, 2009, 10:37am UTC](https://discuss.rubyonrails.org/t/how-to-call-method-declared-in-one-model/45634/9 "2009-08-31T10:37:51Z")

</div>

Hi Colin

Yes it is my name

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [August 31, 2009, 10:47am UTC](https://discuss.rubyonrails.org/t/how-to-call-method-declared-in-one-model/45634/10 "2009-08-31T10:47:30Z")

</div>

I think we are loosing something in the translation in this conversation.

Colin

---

<div class="post-metadata">

**Author:** ![karthik\_k](https://avatars.discourse-cdn.com/v4/letter/k/f0a364/32.png) [@karthik\_k](https://discuss.rubyonrails.org/u/karthik_k)\
**Post date:** [September 1, 2009, 3:29am UTC](https://discuss.rubyonrails.org/t/how-to-call-method-declared-in-one-model/45634/11 "2009-09-01T03:29:11Z")

</div>

Hi

Same old model problem

role.rd //model def check\_role puts “Role sdfsdfd” end

presenter.rb //model def validate\_name  
self.role.check\_role //calling from role.rb model

end

in presenter\_controller i am calling as

@presenter= Presenter.new @presenter.validate\_name

i am getting as

Action Controller: Exception caught

# NoMethodError in PresenterController#create

```ruby
undefined method `role' for #<Presenter:0x2f3d2dc>

```

but in the existing code it is working for some other module

plz Guide me

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [September 1, 2009, 3:54am UTC](https://discuss.rubyonrails.org/t/how-to-call-method-declared-in-one-model/45634/12 "2009-09-01T03:54:17Z")

</div>

> Hi
> 
> Same old model problem
> 
> role.rd //model def check\_role puts "Role sdfsdfd" end
> 
> presenter.rb //model def validate\_name self.role.check\_role //calling from role.rb model end
> 
> in presenter\_controller i am calling as
> 
> @presenter= Presenter.new @presenter.validate\_name
> 
> i am getting as
> 
> Action Controller: Exception caughtNoMethodError in PresenterController#create
> 
> undefined method `role' for #\<Presenter:0x2f3d2dc\>
> 
> but in the existing code it is working for some other module

Does Presenter have a role method ? if not then you've got your answer.

Fred

---

<div class="post-metadata">

**Author:** ![karthik\_k](https://avatars.discourse-cdn.com/v4/letter/k/f0a364/32.png) [@karthik\_k](https://discuss.rubyonrails.org/u/karthik_k)\
**Post date:** [September 1, 2009, 4:09am UTC](https://discuss.rubyonrails.org/t/how-to-call-method-declared-in-one-model/45634/13 "2009-09-01T04:09:00Z")

</div>

hi Fred

I am not able to under stand

Does Presenter have a role method ?

no there is no method in Presenter model

can you plz explain in detail

---

<div class="post-metadata">

**Author:** ![karthik\_k](https://avatars.discourse-cdn.com/v4/letter/k/f0a364/32.png) [@karthik\_k](https://discuss.rubyonrails.org/u/karthik_k)\
**Post date:** [September 1, 2009, 5:34am UTC](https://discuss.rubyonrails.org/t/how-to-call-method-declared-in-one-model/45634/14 "2009-09-01T05:34:17Z")

</div>

Hi Fred

Thank you

I did

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [September 1, 2009, 9:02am UTC](https://discuss.rubyonrails.org/t/how-to-call-method-declared-in-one-model/45634/15 "2009-09-01T09:02:43Z")

</div>

Karthik, I have a two suggestions which I would like you consider very seriously.

1. Get hold of the Pragmatic Bookshelf book Programming Ruby: The Pragmatic Programmer's Guide, downloadable as an e-book. Spend a week working through it till you have a good grasp of Ruby.

2. Get the book Agile Web Development with Rails, 3rd Edition and spend another week (or more) working through that to give you a good grasp of Rails.

This may seem like a lot of wasted time but in the long run it will save you (and us) time and effort and will allow you develop reliable applications much more effectively than you can at the moment.

Colin

---

<div class="post-metadata">

**Author:** ![karthik\_k](https://avatars.discourse-cdn.com/v4/letter/k/f0a364/32.png) [@karthik\_k](https://discuss.rubyonrails.org/u/karthik_k)\
**Post date:** [September 1, 2009, 10:20am UTC](https://discuss.rubyonrails.org/t/how-to-call-method-declared-in-one-model/45634/16 "2009-09-01T10:20:35Z")

</div>

Hi Colin

I did my self

and found the solution

Thank you for your advice
