# Better way for select list for belongs\_to?

**URL:** <https://www.ruby-forum.com/t/better-way-for-select-list-for-belongs-to/121875>\
**Category:** Rails\
**Created:** [December 2, 2007, 9:25am UTC](https://www.ruby-forum.com/t/better-way-for-select-list-for-belongs-to/121875 "2007-12-02T09:25:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [December 2, 2007, 9:25am UTC](https://www.ruby-forum.com/t/better-way-for-select-list-for-belongs-to/121875/1 "2007-12-02T09:25:13Z")

</div>

I’m currently doing all of this just to have a drop-down list of  
Courses for a particular student:

# ===== edit template ===== \<%= f.select :course, @courses, :selected =\> @student.course.id %\>

===== students controller =====  
def edit  
@student = Student.find(params[:id])  
@page\_title = “Edit #{@student.full\_name}”  
@courses = Course.find(:all).collect { |c| [c.name, c.id] }  
end

def update  
@student = Student.find(params[:id])  
@student.attributes = params[:student]  
@student.course = Course.find(params[:student][:course])

```
respond_to do |format|
  if @student.save
    flash[:notice] = "#{@student.full_name} was successfully

```

# updated." format.html { redirect\_to students\_url } format.xml { head :ok } else format.html { render :action =\> “edit” } format.xml { render :xml =\> @student.errors.to\_xml } end end end

Students :belongs\_to :course, and Course :has\_many :students. I’ve  
tried several different ways of doing this, and this has been the  
first that’s actually successful. Having already tried the bad way,  
I’d like to think this is the ugly way, and hope someone has a  
suggestion on the Good Way? 🙂 Any help would be appreciated.

Thanks!

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [December 2, 2007, 4:43pm UTC](https://www.ruby-forum.com/t/better-way-for-select-list-for-belongs-to/121875/2 "2007-12-02T16:43:08Z")

</div>

How about:

# ===== edit template ===== \<%= f.select :course\_id, @courses %\>

===== students controller =====  
def edit  
@student = Student.find(params[:id])  
@page\_title = “Edit #{@student.full\_name}”  
@courses = Course.find(:all).collect { |c| [c.name, c.id] }  
end

def update  
@student = Student.find(params[:id])  
@student.attributes = params[:student]

```
respond_to do |format|
  if @student.save
    flash[:notice] = "#{@student.full_name} was successfully

```

# updated." format.html { redirect\_to students\_url } format.xml { head :ok } else format.html { render :action =\> “edit” } format.xml { render :xml =\> @student.errors.to\_xml } end end end

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [December 2, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/better-way-for-select-list-for-belongs-to/121875/3 "2007-12-02T18:46:14Z")

</div>

When I get that, I get an exception: Course expected, but got String

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [December 2, 2007, 8:11pm UTC](https://www.ruby-forum.com/t/better-way-for-select-list-for-belongs-to/121875/4 "2007-12-02T20:11:49Z")

</div>

Oops… forgot to mark attr\_accessible :course\_id. 🙂 Doing what you  
suggested works as expected. I tried a lot of different combinations  
to get what I wanted, I just must have missed this one. Thanks!
