# Using select to set a relationship class

**URL:** <https://www.ruby-forum.com/t/using-select-to-set-a-relationship-class/127538>\
**Category:** Rails\
**Created:** [January 30, 2008, 5:36am UTC](https://www.ruby-forum.com/t/using-select-to-set-a-relationship-class/127538 "2008-01-30T05:36:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![apswartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/71e660/32.png) [@apswartz](https://www.ruby-forum.com/u/apswartz)\
**Post date:** [January 30, 2008, 5:36am UTC](https://www.ruby-forum.com/t/using-select-to-set-a-relationship-class/127538/1 "2008-01-30T05:36:37Z")

</div>

I have two models:Task and Context

Task belongs\_to Context  
Context has\_many Tasks

In my form partial for editing a task, I want to have a drop down with  
all  
of the contexts where you can select one for the task.

I tried:  
**Contexts:** \<%= collection\_select(“task”, “context” , @contexts,  
“id”,  
“fullName”, {}) %\>

which doesn’t work for setting with the current value.

Any suggestions?

Thanks in advance,

Alan

---

<div class="post-metadata">

**Author:** ![apswartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/71e660/32.png) [@apswartz](https://www.ruby-forum.com/u/apswartz)\
**Post date:** [January 30, 2008, 7:30am UTC](https://www.ruby-forum.com/t/using-select-to-set-a-relationship-class/127538/2 "2008-01-30T07:30:34Z")

</div>

Try changing the “id” param name in the collection\_select method to  
“context\_id”, or whatever the name of the foreign key is within task  
model.

Let me know if that works.

---

<div class="post-metadata">

**Author:** ![apswartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/71e660/32.png) [@apswartz](https://www.ruby-forum.com/u/apswartz)\
**Post date:** [January 31, 2008, 1:34am UTC](https://www.ruby-forum.com/t/using-select-to-set-a-relationship-class/127538/3 "2008-01-31T01:34:48Z")

</div>

Close. The solution I got to work was: **Contexts:** \<%=  
collection\_select(“task”, “context\_id” , @contexts, “id”, “fullName”,  
{}) %\>

Thanks for your help!!

–Alan

On Jan 30, 2008 12:30 AM, Chris O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
