# Accessing method in another controller

**URL:** <https://discuss.rubyonrails.org/t/accessing-method-in-another-controller/50830>\
**Category:** rubyonrails-talk\
**Created:** [April 12, 2010, 11:31am UTC](https://discuss.rubyonrails.org/t/accessing-method-in-another-controller/50830 "2010-04-12T11:31:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ike](https://avatars.discourse-cdn.com/v4/letter/i/df705f/32.png) [@Ike](https://discuss.rubyonrails.org/u/Ike)\
**Post date:** [April 12, 2010, 11:31am UTC](https://discuss.rubyonrails.org/t/accessing-method-in-another-controller/50830/1 "2010-04-12T11:31:24Z")

</div>

If I have :

class X \< ApplicationController &nbsp;&nbsp;&nbsp;def some\_method &nbsp;&nbsp;... &nbsp;&nbsp;&nbsp;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:** ![Ike](https://avatars.discourse-cdn.com/v4/letter/i/df705f/32.png) [@Ike](https://discuss.rubyonrails.org/u/Ike)\
**Post date:** [April 12, 2010, 2:07pm UTC](https://discuss.rubyonrails.org/t/accessing-method-in-another-controller/50830/2 "2010-04-12T14:07:05Z")

</div>

Seems the best solution is the straight OOP solution:

class Y \< X &nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;&nbsp;something = some\_method &nbsp;&nbsp;&nbsp;.... end

---

<div class="post-metadata">

**Author:** ![Andy\_Jeffries1](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@Andy\_Jeffries1](https://discuss.rubyonrails.org/u/Andy_Jeffries1)\
**Post date:** [April 12, 2010, 2:46pm UTC](https://discuss.rubyonrails.org/t/accessing-method-in-another-controller/50830/3 "2010-04-12T14:46:52Z")

</div>

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:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 12, 2010, 4:20pm UTC](https://discuss.rubyonrails.org/t/accessing-method-in-another-controller/50830/4 "2010-04-12T16:20:53Z")

</div>

Andy Jeffries 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 &nbsp;&nbsp;def some\_method &nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;end end
> 
> class X \< Z &nbsp;&nbsp;... end
> 
> class Y \< Z &nbsp;&nbsp;... end

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

> module Z &nbsp;&nbsp;def some\_method &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;end end
> 
> class X \< ApplicationController &nbsp;&nbsp;include Z &nbsp;&nbsp;... end
> 
> class Y \< ApplicationController &nbsp;&nbsp;include Z &nbsp;&nbsp;... 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...
