# Associations don't work with FormHelpers?

**URL:** <https://www.ruby-forum.com/t/associations-dont-work-with-formhelpers/71381>\
**Category:** Rails\
**Created:** [October 8, 2006, 2:07am UTC](https://www.ruby-forum.com/t/associations-dont-work-with-formhelpers/71381 "2006-10-08T02:07:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 8, 2006, 2:07am UTC](https://www.ruby-forum.com/t/associations-dont-work-with-formhelpers/71381/1 "2006-10-08T02:07:14Z")

</div>

Hi,

I am trying to use an association in a form helper, like this:

\<%= text\_field “group.moderator”, “id” %\>

I get this error:  
`@group.moderator’ is not allowed as an instance variable name

Here is how I defined the models:

class Moderator \< ActiveRecord::Base  
has\_many :groups  
end

class Group \< ActiveRecord::Base  
belongs\_to :moderator  
end

How can I use associations in form helpers?

Thanks,

David

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 8, 2006, 10:15am UTC](https://www.ruby-forum.com/t/associations-dont-work-with-formhelpers/71381/2 "2006-10-08T10:15:45Z")

</div>

On Oct 8, 2006, at 2:05 AM, removed\_email\_address@domain.invalid wrote:

> ```
> has_many :groups
> 
> ```
> 
> end
> 
> class Group \< ActiveRecord::Base  
> belongs\_to :moderator  
> end

The call is

\<%= text\_field :group, :moderator\_id %\>

assuming that’s the foreign key in “groups”. Nevertheless it is  
unsual to ask the user for an ID, normally you use a combo.

If the number of groups is too large for the combo to be usable some  
kind of auto completer is handy, but strings are not good to identify  
models. I wrote a helper that encapsulates a widget that mixes the  
auto completer with a hidden field for the key, I plan to polish it a  
release it as a plugin.

– fxn

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 9, 2006, 5:26am UTC](https://www.ruby-forum.com/t/associations-dont-work-with-formhelpers/71381/3 "2006-10-09T05:26:52Z")

</div>

Xavier N. wrote:

> > 
> 
> \<%= text\_field :group, :moderator\_id %\>  
> – fxn  
> Actually, I want to display other fields in the moderators table, and  
> traverse the association, like this:  
> \<%= text\_field “group.moderator”, “login” %\> or \<%= text\_field  
> “group”, “moderator.login” %\>  
> where “login” is an atttribute of the “moderator” which is associated  
> with the “group”.  
> Neither of these approaches works, but “@group.moderator.login” is a  
> valid attribute.

Any ideas?

Thanks,

David

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 9, 2006, 8:27am UTC](https://www.ruby-forum.com/t/associations-dont-work-with-formhelpers/71381/4 "2006-10-09T08:27:10Z")

</div>

On Oct 9, 2006, at 5:24 AM, removed\_email\_address@domain.invalid wrote:

> Actually, I want to display other fields in the moderators table, and  
> traverse the association, like this:  
> \<%= text\_field “group.moderator”, “login” %\> or \<%= text\_field  
> “group”, “moderator.login” %\>  
> where “login” is an atttribute of the “moderator” which is associated  
> with the “group”.  
> Neither of these approaches works, but “@group.moderator.login” is a  
> valid attribute.

Helpers like text\_field have this signature

text\_field(object, method, …)

where “object” is the name of an instance variable in the view  
(idiomatically a symbol), and “method” the name of an accessor.  
Behind the scenes, the helper gets the variable dynamically and sends  
() method to it. With that call the helper sets the initial value of  
the widget, and constructs the name “object[method]”. That’s very  
handy, because most of the time is just what you need.

You need to switch to the generic \*\_tag family of helpers for extra  
flexibility, in this case text\_field\_tag.

– fxn
