# How to process collection\_select in controller?

**URL:** <https://www.ruby-forum.com/t/how-to-process-collection-select-in-controller/184393>\
**Category:** Rails\
**Created:** [March 1, 2010, 10:17pm UTC](https://www.ruby-forum.com/t/how-to-process-collection-select-in-controller/184393 "2010-03-01T22:17:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thilo-Alexander\_Gin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3e96dc/32.png) [@Thilo-Alexander\_Gin](https://www.ruby-forum.com/u/Thilo-Alexander_Gin)\
**Post date:** [March 1, 2010, 10:17pm UTC](https://www.ruby-forum.com/t/how-to-process-collection-select-in-controller/184393/1 "2010-03-01T22:17:24Z")

</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  
belongs\_to :reg\_template, :class\_name =\> “EmailTemplate”  
[…]  
end

class EmailTemplate \< ActiveRecord::Base  
has\_many :events  
[…]  
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:  
unknown attribute: reg\_template\_id  
when attempting to call  
Event.update\_attributes

Using  
\<%= f.collection\_select(:reg\_template, EmailTemplate.all, :id, :name) %\>  
instead also does not help much as it will fail with:  
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:** ![Thilo-Alexander\_Gin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3e96dc/32.png) [@Thilo-Alexander\_Gin](https://www.ruby-forum.com/u/Thilo-Alexander_Gin)\
**Post date:** [March 2, 2010, 12:00pm UTC](https://www.ruby-forum.com/t/how-to-process-collection-select-in-controller/184393/2 "2010-03-02T12:00:22Z")

</div>

On Mon, Mar 1, 2010 at 10:15 PM, Thilo-Alexander Ginkel  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> What is the recommended way of processing this form field in an action  
> Using

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 ?

–  
Gael Muller

---

<div class="post-metadata">

**Author:** ![Thilo-Alexander\_Gin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3e96dc/32.png) [@Thilo-Alexander\_Gin](https://www.ruby-forum.com/u/Thilo-Alexander_Gin)\
**Post date:** [March 2, 2010, 12:06pm UTC](https://www.ruby-forum.com/t/how-to-process-collection-select-in-controller/184393/3 "2010-03-02T12:06:49Z")

</div>

use this

\<%= collection\_select(:book, :subject\_id,  
@subjects, :id, :name) %\>

some thing like this or

# Edit Book Detail
\<% form\_tag :action =\> 'update', :id =\> @book do %\>

Title: \<%= text\_field 'book', 'title' %\>

Price: \<%= text\_field 'book', 'price' %\>

Subject: \<%= collection\_select(:book, :subject\_id, @subjects, :id, :name) %\>

Description  
\<%= text\_area 'book', 'description' %\>

\<%= submit\_tag "Save changes" %\> \<% end %\> \<%= link\_to 'Back', {:action =\> 'list' } %\>

its just an idea

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

On Tue, Mar 2, 2010 at 3:05 PM, Gael Muller  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> > class Event \< ActiveRecord::Base
> > 
> > do for a 1:n relationship), so attempts to read or assign this field will
> 
> Hello Thilo,  
> “Ruby on Rails: Talk” group.  
> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to  
> removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
Thanks:  
Rajeev sharma

---

<div class="post-metadata">

**Author:** ![Thilo-Alexander\_Gin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3e96dc/32.png) [@Thilo-Alexander\_Gin](https://www.ruby-forum.com/u/Thilo-Alexander_Gin)\
**Post date:** [March 7, 2010, 3:41pm UTC](https://www.ruby-forum.com/t/how-to-process-collection-select-in-controller/184393/4 "2010-03-07T15:41:58Z")

</div>

Hello Gael,

On Tuesday 02 March 2010 10:35:26 Gael Muller wrote:

> 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
