# Invoking methods

**URL:** <https://www.ruby-forum.com/t/invoking-methods/153289>\
**Category:** Rails\
**Created:** [November 14, 2008, 11:57am UTC](https://www.ruby-forum.com/t/invoking-methods/153289 "2008-11-14T11:57:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@mad](https://www.ruby-forum.com/u/mad)\
**Post date:** [November 14, 2008, 11:57am UTC](https://www.ruby-forum.com/t/invoking-methods/153289/1 "2008-11-14T11:57:57Z")

</div>

hi everyone,

i got a simple question that’s bothering me all day:  
is it possible to find out which method and controller did the  
original call?

to make this a little clearer, I want to do the following:  
(let’s say these two methods are in different controllers)

def first\_method  
say\_my\_name  
end

def say\_my\_name

# how do i get the original method and controller

# invoking\_method = ?

# invoking\_controller =?

puts invoking\_method  
puts invoking\_controller  
end

any help is appreciated!

---

<div class="post-metadata">

**Author:** ![mad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@mad](https://www.ruby-forum.com/u/mad)\
**Post date:** [November 14, 2008, 12:09pm UTC](https://www.ruby-forum.com/t/invoking-methods/153289/2 "2008-11-14T12:09:05Z")

</div>

On 14 Nov 2008, at 10:57, MaD wrote:

> hi everyone,
> 
> i got a simple question that’s bothering me all day:  
> is it possible to find out which method and controller did the  
> original call?

You can screw around with caller but that’s not a very clean way to go  
about things.

Fred

---

<div class="post-metadata">

**Author:** ![mad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@mad](https://www.ruby-forum.com/u/mad)\
**Post date:** [November 14, 2008, 12:28pm UTC](https://www.ruby-forum.com/t/invoking-methods/153289/3 "2008-11-14T12:28:28Z")

</div>

thanks!

i just needed it for some log-entries anyway. so there is no need to  
find a cleaner way.

On 14 Nov., 12:08, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@mad](https://www.ruby-forum.com/u/mad)\
**Post date:** [November 14, 2008, 12:31pm UTC](https://www.ruby-forum.com/t/invoking-methods/153289/4 "2008-11-14T12:31:24Z")

</div>

As the OP was speaking of controllers: yes, you can. Check  
controller\_name and action\_name.

But then:

> > to make this a little clearer, I want to do the following:  
> > (let’s say these two methods are in different controllers)

I see no way to have one controller calling instance methods on  
another controller. You could achieve this with components, but as the  
Rails docs state: you shouldn’t.

/eno

====================================================================  
A wee piece of ruby every monday: [http://1rad.wordpress.com/](http://1rad.wordpress.com/)

---

<div class="post-metadata">

**Author:** ![mad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@mad](https://www.ruby-forum.com/u/mad)\
**Post date:** [November 14, 2008, 1:07pm UTC](https://www.ruby-forum.com/t/invoking-methods/153289/5 "2008-11-14T13:07:12Z")

</div>

actually, never mind.

in my initializer

self.controller\_name  
self.action\_name

still return the names of my calling controller and action. guess that  
was just too easy to do in the first place.

---

<div class="post-metadata">

**Author:** ![mad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@mad](https://www.ruby-forum.com/u/mad)\
**Post date:** [November 14, 2008, 12:44pm UTC](https://www.ruby-forum.com/t/invoking-methods/153289/6 "2008-11-14T12:44:55Z")

</div>

yes you are right. my example was wrong.  
actually the second method ain’t in an controller, but in an  
initializer. and what i really wanted to do is log the calling  
controller\_name and method\_name. but i wanted to achieve this without  
having to add more parameters to my method call.

of course i could just say:  
say\_my\_name(self.controller\_name, self.method\_name)

but i’m lazy and i don’t want to type “(self.controller\_name,  
self.method\_name)” all the time, so i thought tried to find a way to  
figure out these infos inside the say\_my\_name-method.

if there ain’t anything better than to parse caller, i’m gonna go with  
that.

On 14 Nov., 12:30, “Enrico Thierbach”
