# Text\_field with has\_many, :through =\>

**URL:** <https://www.ruby-forum.com/t/text-field-with-has-many-through/147971>\
**Category:** Rails\
**Created:** [September 7, 2008, 9:19pm UTC](https://www.ruby-forum.com/t/text-field-with-has-many-through/147971 "2008-09-07T21:19:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [September 7, 2008, 9:19pm UTC](https://www.ruby-forum.com/t/text-field-with-has-many-through/147971/1 "2008-09-07T21:19:32Z")

</div>

This should be a really simple thing, I just haven’t figured out how to  
do it yet…

I have two models with a has\_many, :through =\> relationship:

class Source \< ActiveRecord::Base  
has\_many :authorships  
has\_many :authors, :through =\> :authorships

class Author \< ActiveRecord::Base  
has\_many :authorships  
has\_many :sources, :through =\> :authorships

class Authorship \< ActiveRecord::Base  
belongs\_to :source  
belongs\_to :author

I’m not sure how to create a form for this. My sources/new form has a  
nested form for authorship that currently looks like this (I’m following  
the instructions Ryan B. gives in _Advanced Rails Recipes_ for  
dealing with multiple models in one form):

\<% new\_or\_existing = authorship.new\_record? ? ‘new’ : ‘existing’ %\>  
\<% prefix = “source[#{new\_or\_existing}\_authorship\_attributes][]” %\>

\<% fields\_for prefix, authorship do |authorship\_form| %\>

\<%= authorship\_form.text\_field :author\_id %\> \<%= link\_to\_function "remove", "$(this).up('.authorship').remove()" %\>

\<% end %\>

This works just fine, and saves the data just fine, but obviously I  
would like to be able to type in author.name instead of :author\_id. I  
would also be happy to use collection\_select here, but haven’t been able  
to figure out how to do that with a has\_many, :through =\> relationship.

Thank you!
