# Select html for habtm

**URL:** <https://www.ruby-forum.com/t/select-html-for-habtm/91386>\
**Category:** Rails\
**Created:** [April 8, 2007, 2:47am UTC](https://www.ruby-forum.com/t/select-html-for-habtm/91386 "2007-04-08T02:47:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [April 8, 2007, 2:47am UTC](https://www.ruby-forum.com/t/select-html-for-habtm/91386/1 "2007-04-08T02:47:44Z")

</div>

How does one use a for a habtm field? I’m having trouble  
getting it to work.

Here’s my html:

Project  
 \<%= options\_from\_collection\_for\_select(@all\_projects, :id, :name, @selected\_projects) %\>

And my controller:

def edit  
@schedule = Schedule.find(params[:id])  
@all\_projects = Project.find(:all, :order=\>“name”)  
@selected\_projects = @schedule.projects.collect{|prj|  
prj.project\_id.to\_i}  
end

def update  
@schedule = Schedule.find(params[:id])  
if @schedule.update\_attributes(params[:schedule])  
flash[:notice] = ‘Schedule was successfully updated.’  
redirect\_to :action =\> ‘show’, :id =\> @schedule  
else  
render :action =\> ‘edit’  
end  
end

I keep getting an error:

ActiveRecord::StatementInvalid in ScheduleController#update

Mysql::Error: Duplicate entry ‘3’ for key 1: INSERT INTO  
projects\_schedules (`project_id`, `id`, `schedule_id`) VALUES (3, 3,  
1)

It’s because the id 3 is already used in the projects\_schedules table.  
Why is it choosing a duplicate key?

T.
