# Pulling my hair out: complex collection\_select

**URL:** <https://www.ruby-forum.com/t/pulling-my-hair-out-complex-collection-select/134040>\
**Category:** Rails\
**Created:** [April 7, 2008, 8:48am UTC](https://www.ruby-forum.com/t/pulling-my-hair-out-complex-collection-select/134040 "2008-04-07T08:48:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdrietz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5f8ce5/32.png) [@jdrietz](https://www.ruby-forum.com/u/jdrietz)\
**Post date:** [April 7, 2008, 8:48am UTC](https://www.ruby-forum.com/t/pulling-my-hair-out-complex-collection-select/134040/1 "2008-04-07T08:48:39Z")

</div>

I’ve been trying to extend Ryan B.’ great recipe from Advanced Rails  
Recipes called “Handle Multiple Models In One Form”. I got everything  
in my “base” functionality to work in about 30 minutes and I’ve spent  
the last 4 days trying to figure out how to use collection\_select in the  
forms - very frustrating!

The basic idea of his recipe is here:

> **[\#75 Complex Forms Part 3 - RailsCasts](http://railscasts.com/episodes/75-complex-forms-part-3)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

I’m building a simple address book so I have models Person and  
EmailAddress where a person has\_many email\_addresses. My edit form,  
like Ryan’s, has the ability to add new fields with page.insert\_html and  
to remove existing entries. All’s fine up to this point.

What I can’t do is create a drop-down menu with a simple list of options  
to tag the email. Here’s what I think should work in my email partial  
(called from \<%= render :partial =\> ‘person/email’, :collection =\>  
@person.email\_addresses %\>) :

\<% new\_or\_existing = email.new\_record? ? ‘new’ : ‘existing’ %\>  
\<% prefix = “person[#{new\_or\_existing}\_email\_attributes][]”%\>  
\<% fields\_for prefix, email do |email\_form| %\>  
\<%= email\_form.text\_field :email %\>&nbsp;  
\<%= email\_form.collection\_select(:person, “email\_attributes”,  
EmailAddress::VALID\_EMAIL\_TAGS, “to\_s”, “to\_s”) %\>  
[…snip…]  
\<% end %\>

The error I get is “undefined method `merge’ for “to\_s”:String”

If I replace the collection\_select call with this:  
  
\<%= options\_from\_collection\_for\_select(EmailAddress::VALID\_EMAIL\_TAGS,  
“to\_s”,  
“to\_s”,  
email.tag) %\>

I get as many email partials rendered as there are items in the  
collection (and the dynamic insert\_html works too). The problem is that  
all the select input fields are named “test” and I don’t get them passed  
in my params hash as part of the EmailAddress record. This breaks the  
mass assignment possibilities and ruins so much of the benefit of Ryan’s  
recipe.

The text field works great and I get one of these two kinds of params:  
person[existing\_email\_attributes][4][tag]  
person[new\_email\_attributes][][tag]

Any ideas?

---

<div class="post-metadata">

**Author:** ![jdrietz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5f8ce5/32.png) [@jdrietz](https://www.ruby-forum.com/u/jdrietz)\
**Post date:** [April 7, 2008, 9:06am UTC](https://www.ruby-forum.com/t/pulling-my-hair-out-complex-collection-select/134040/2 "2008-04-07T09:06:46Z")

</div>

Justin Rowe wrote:

> I’ve been trying to extend Ryan B.’ great recipe from Advanced Rails  
> Recipes called “Handle Multiple Models In One Form”. I got everything  
> in my “base” functionality to work in about 30 minutes and I’ve spent  
> the last 4 days trying to figure out how to use collection\_select in the  
> forms - very frustrating!

5 minute rule. I post and then I get it working. Basically I gave up  
on getting a helper function to work for the field name and just created  
it myself and used the options\_from\_collection\_for\_select instead:

\<%=  
options\_from\_collection\_for\_select(EmailAddress::VALID\_EMAIL\_TAGS,  
“to\_s”,  
“to\_s”,  
email.tag) %\>

Oy.
