# Using helper method of another model

**URL:** <https://www.ruby-forum.com/t/using-helper-method-of-another-model/57040>\
**Category:** Rails\
**Created:** [April 6, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/using-helper-method-of-another-model/57040 "2006-04-06T19:22:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![r00by\_n00by](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/839c29/32.png) [@r00by\_n00by](https://www.ruby-forum.com/u/r00by_n00by)\
**Post date:** [April 6, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/using-helper-method-of-another-model/57040/1 "2006-04-06T19:22:59Z")

</div>

I have a template that belongs to a certain model (model A). I need to  
call a helper method of a different model (model B). Then I need the  
helper method of B to call a helper method of model C.  
I tried using “include” and “require” and using namespaces, but I got  
“undefined method”. What is the way to do it?

---

<div class="post-metadata">

**Author:** ![r00by\_n00by](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/839c29/32.png) [@r00by\_n00by](https://www.ruby-forum.com/u/r00by_n00by)\
**Post date:** [April 6, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/using-helper-method-of-another-model/57040/2 "2006-04-06T20:26:44Z")

</div>

r00by n00by wrote:

> I have a template that belongs to a certain model (model A). I need to  
> call a helper method of a different model (model B). Then I need the  
> helper method of B to call a helper method of model C.  
> I tried using “include” and “require” and using namespaces, but I got  
> “undefined method”. What is the way to do it?

Disclaimer: a newb writes:  
Try helper :modelB in your A controller to allow B’s helper to be  
accessible in A. Not sure bout the nesting, but might work.  
Alternatively, i think if you put the helper methods in  
application\_helper.rb they should be able to see each other.  
Hope this helps  
Chris T

---

<div class="post-metadata">

**Author:** ![r00by\_n00by](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/839c29/32.png) [@r00by\_n00by](https://www.ruby-forum.com/u/r00by_n00by)\
**Post date:** [April 6, 2006, 11:07pm UTC](https://www.ruby-forum.com/t/using-helper-method-of-another-model/57040/3 "2006-04-06T23:07:43Z")

</div>

Chris T wrote:

> r00by n00by wrote:
> 
> > I have a template that belongs to a certain model (model A). I need to  
> > call a helper method of a different model (model B). Then I need the  
> > helper method of B to call a helper method of model C.  
> > I tried using “include” and “require” and using namespaces, but I got  
> > “undefined method”. What is the way to do it?
> 
> Disclaimer: a newb writes:  
> Try helper :modelB in your A controller to allow B’s helper to be  
> accessible in A. Not sure bout the nesting, but might work.  
> Alternatively, i think if you put the helper methods in  
> application\_helper.rb they should be able to see each other.  
> Hope this helps  
> Chris T

I tried “helper :modelB” and it did help a little. I can now call  
BHelper.foo from a template that belongs to A , but now I have  
“undefined method” when I try to call link\_to inside BHelper.foo .  
AHelper also has a method called “foo” so I can’t drop the namespace.

---

<div class="post-metadata">

**Author:** ![r00by\_n00by](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/839c29/32.png) [@r00by\_n00by](https://www.ruby-forum.com/u/r00by_n00by)\
**Post date:** [April 7, 2006, 12:07am UTC](https://www.ruby-forum.com/t/using-helper-method-of-another-model/57040/4 "2006-04-07T00:07:42Z")

</div>

r00by n00by wrote:

> I have a template that belongs to a certain model (model A). I need to  
> call a helper method of a different model (model B). Then I need the  
> helper method of B to call a helper method of model C.  
> I tried using “include” and “require” and using namespaces, but I got  
> “undefined method”. What is the way to do it?

Put the methods you need across models in the ApplicationHelper.

---

<div class="post-metadata">

**Author:** ![r00by\_n00by](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/839c29/32.png) [@r00by\_n00by](https://www.ruby-forum.com/u/r00by_n00by)\
**Post date:** [April 7, 2006, 12:35am UTC](https://www.ruby-forum.com/t/using-helper-method-of-another-model/57040/5 "2006-04-07T00:35:09Z")

</div>

These are good solutions and maybe I will use them, but I would still  
like to know how to call link\_to from BHelper.foo (using namespace when  
defining the method). I thought ActionView::Helpers::UrlHelper::link\_to  
should work. It didn’t.

---

<div class="post-metadata">

**Author:** ![r00by\_n00by](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/839c29/32.png) [@r00by\_n00by](https://www.ruby-forum.com/u/r00by_n00by)\
**Post date:** [April 7, 2006, 12:04am UTC](https://www.ruby-forum.com/t/using-helper-method-of-another-model/57040/6 "2006-04-07T00:04:14Z")

</div>

r00by n00by wrote:

> > >
> 
> I tried “helper :modelB” and it did help a little. I can now call  
> BHelper.foo from a template that belongs to A , but now I have  
> “undefined method” when I try to call link\_to inside BHelper.foo .  
> AHelper also has a method called “foo” so I can’t drop the namespace.

Not sure, but can you refer to fully qualified names ModelA :: foo  
(sorry don’t have Agile book to hand at the moment). Alternatively,  
maybe you will just have to rename some of the methods afoo, bfoo etc.  
Sorry can’t be more of a help at the moment.  
Chris T
