# How to process collection\_select in controller?

**URL:** <https://discuss.rubyonrails.org/t/how-to-process-collection-select-in-controller/49799>\
**Category:** rubyonrails-talk\
**Created:** [March 1, 2010, 9:15pm UTC](https://discuss.rubyonrails.org/t/how-to-process-collection-select-in-controller/49799 "2010-03-01T21:15:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thilo-Alexander\_Gink](https://avatars.discourse-cdn.com/v4/letter/t/ce73a5/32.png) [@Thilo-Alexander\_Gink](https://discuss.rubyonrails.org/u/Thilo-Alexander_Gink)\
**Post date:** [March 1, 2010, 9:15pm UTC](https://discuss.rubyonrails.org/t/how-to-process-collection-select-in-controller/49799/1 "2010-03-01T21:15:18Z")

</div>

Hello everyone,

I am currently somewhat stuck figuring out an elegant solution to my following problem:

Let's say I have the following classes:

class Event \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :reg\_template, :class\_name =\> "EmailTemplate" &nbsp;&nbsp;[...] end

class EmailTemplate \< ActiveRecord::Base &nbsp;&nbsp;has\_many :events &nbsp;&nbsp;[...] end

And a view that contains:

\<%= f.collection\_select(:reg\_template\_id, EmailTemplate.all, :id, :name) %\>

What is the recommended way of processing this form field in an action controller?

Having a 1:1 relationship between Event and EmailTemplate means that Rails does not generate a reg\_template\_id and reg\_template\_id= method (as it would do for a 1:n relationship), so attempts to read or assign this field will fail with: &nbsp;&nbsp;unknown attribute: reg\_template\_id when attempting to call &nbsp;&nbsp;Event.update\_attributes

Using \<%= f.collection\_select(:reg\_template, EmailTemplate.all, :id, :name) %\> instead also does not help much as it will fail with: &nbsp;&nbsp;EmailTemplate(#70070455907700) expected, got String(#70070510199800)

I guess I must be missing something terribly obvious as I think is is rather common to update a model instance with a reference to another object through a collection\_select.

Thanks & kind regards, Thilo

---

<div class="post-metadata">

**Author:** ![Gael\_Muller](https://avatars.discourse-cdn.com/v4/letter/g/a9a28c/32.png) [@Gael\_Muller](https://discuss.rubyonrails.org/u/Gael_Muller)\
**Post date:** [March 2, 2010, 9:35am UTC](https://discuss.rubyonrails.org/t/how-to-process-collection-select-in-controller/49799/2 "2010-03-02T09:35:26Z")

</div>

Hello Thilo,

In my opinion, there is nothing wrong with your piece of code. Are you sure you have a reg\_template\_id column in your table ? Could you show us the migration for the Event class ?

---

<div class="post-metadata">

**Author:** ![Rajeev\_Kannav\_Sharma](https://avatars.discourse-cdn.com/v4/letter/r/8dc957/32.png) [@Rajeev\_Kannav\_Sharma](https://discuss.rubyonrails.org/u/Rajeev_Kannav_Sharma)\
**Post date:** [March 2, 2010, 11:04am UTC](https://discuss.rubyonrails.org/t/how-to-process-collection-select-in-controller/49799/3 "2010-03-02T11:04:59Z")

</div>

use this

```ruby
<%= collection_select(:book, :subject_id,
                         @subjects, :id, :name) %>

some thing like this or

<h1>Edit Book Detail</h1>

<% form_tag :action => 'update', :id => @book do %>
<p><label for="book_title">Title</label>:
<%= text_field 'book', 'title' %></p>
<p><label for="book_price">Price</label>:

<%= text_field 'book', 'price' %></p>
<p><label for="book_subject">Subject</label>:
<%= collection_select(:book, :subject_id,
                         @subjects, :id, :name) %></p>

<p><label for="book_description">Description</label><br/>
<%= text_area 'book', 'description' %></p>
<%= submit_tag "Save changes" %>

<% end %>
<%= link_to 'Back', {:action => 'list' } %>

its just an idea

```

\<%= f.collection\_select(:reg\_ template\_id, EmailTemplate.all, :id, :name) %\>

---

<div class="post-metadata">

**Author:** ![Thilo-Alexander\_Gink](https://avatars.discourse-cdn.com/v4/letter/t/ce73a5/32.png) [@Thilo-Alexander\_Gink](https://discuss.rubyonrails.org/u/Thilo-Alexander_Gink)\
**Post date:** [March 7, 2010, 2:41pm UTC](https://discuss.rubyonrails.org/t/how-to-process-collection-select-in-controller/49799/4 "2010-03-07T14:41:08Z")

</div>

Hello Gael,

> In my opinion, there is nothing wrong with your piece of code. Are you sure you have a reg\_template\_id column in your table ? Could you show us the migration for the Event class ?

sorry for the delayed response - some unexpected events got in my way...

Back to the topic: Well, I had manually added the column to the database table in addition to creating the corresponding migration and apparently missed that the column is supposed to be named reg\_template\_id and not just reg\_template.

With the right column naming everything started working as expected.

Sorry for the confusion and thanks for your hint!

Thanks, Thilo
