# Accessing method in another controller

**URL:** <https://www.ruby-forum.com/t/accessing-method-in-another-controller/186790>\
**Category:** Rails\
**Created:** [April 12, 2010, 1:32pm UTC](https://www.ruby-forum.com/t/accessing-method-in-another-controller/186790 "2010-04-12T13:32:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [April 12, 2010, 1:32pm UTC](https://www.ruby-forum.com/t/accessing-method-in-another-controller/186790/1 "2010-04-12T13:32:59Z")

</div>

If I have :

class X \< ApplicationController  
def some\_method  
…  
end  
end

and I have

class Y \< ApplicationController

end

How, from a method within class Y, can I call class X’s some\_method ?

TIA, RVince

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [April 12, 2010, 4:08pm UTC](https://www.ruby-forum.com/t/accessing-method-in-another-controller/186790/2 "2010-04-12T16:08:43Z")

</div>

Seems the best solution is the straight OOP solution:

class Y \< X  
…  
something = some\_method  
…  
end

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [April 12, 2010, 4:48pm UTC](https://www.ruby-forum.com/t/accessing-method-in-another-controller/186790/3 "2010-04-12T16:48:33Z")

</div>

On 12 April 2010 15:07, RVince [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Seems the best solution is the straight OOP solution:
> 
> class Y \< X  
> …  
> something = some\_method  
> …  
> end

I’d say it depends on whether Y really IS AN X. If not, but they have  
similar roles, I’d say you’re better of doing this:

class Z \< ApplicationController  
def some\_method  
…  
end  
end

class X \< Z  
…  
end

class Y \< Z  
…  
end

or more simply:

module Z  
def some\_method  
…  
end  
end

class X \< ApplicationController  
include Z

…  
end

class Y \< ApplicationController  
include Z

…  
end

You could also instantiate an X controller, but depending on the method  
you’re trying to call, you may need to do a bit of setup (pass in the  
request/response somehow so it can take over).

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [April 12, 2010, 6:20pm UTC](https://www.ruby-forum.com/t/accessing-method-in-another-controller/186790/4 "2010-04-12T18:20:53Z")

</div>

Andy J. wrote:

> I’d say it depends on whether Y really IS AN X. If not, but they have  
> similar roles, I’d say you’re better of doing this:
> 
> class Z \< ApplicationController  
> def some\_method  
> …  
> end  
> end
> 
> class X \< Z  
> …  
> end
> 
> class Y \< Z  
> …  
> end

I like this if ALL your controllers should have access to some\_method

> module Z  
> def some\_method  
> …  
> end  
> end
> 
> class X \< ApplicationController  
> include Z  
> …  
> end
> 
> class Y \< ApplicationController  
> include Z  
> …  
> end

I like this if some\_method should only be available to some controllers.

Depends on the application requirements as to which way I would go…
