# Has\_many through select box in form

**URL:** <https://www.ruby-forum.com/t/has-many-through-select-box-in-form/153646>\
**Category:** Rails\
**Created:** [November 19, 2008, 7:15pm UTC](https://www.ruby-forum.com/t/has-many-through-select-box-in-form/153646 "2008-11-19T19:15:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![cjharrelson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c89c15/32.png) [@cjharrelson](https://www.ruby-forum.com/u/cjharrelson)\
**Post date:** [November 19, 2008, 7:15pm UTC](https://www.ruby-forum.com/t/has-many-through-select-box-in-form/153646/1 "2008-11-19T19:15:00Z")

</div>

Gentlemen,

I have searched the group and have not found any post that clarifies  
thsi s=issue to me. Please forgive me in advance if I missed  
something.

I have 3 models:

Ticket  
has\_many :citations, :dependent =\> :nullify  
has\_many :violations, :through =\> :citations

Violation  
has\_many :citations, :dependent =\> :nullify  
has\_many :tickets, :through =\> :citations

Citation  
belongs\_to :ticket  
belongs\_to :violation

I am creating the edit a new views for the ticket and would like to be  
able to relate N violations to the ticket. My intent is to have one  
select box with a control to add N more if desired. The problem I am  
running into is getting the select boxes created in a way such that  
(1) existing items select the correct entry in the select box and (2)  
when I post the form, Rails recognizes that I am trying to create N  
relations.

I have tried following examples for a has\_many relationship, but it is  
not working the same.

Following is my current code:

edit.html.erb:

…

\<% form\_for @ticket do |f| %\>  
\<%= render :partial =\> ‘form’, :locals =\> { :f =\> f } %\>  
\<%= submit\_tag ‘Update Ticket’ %\>  
\<% end %\>

…

\_form.html.erb:

…

\<%= render :partial =\> ‘violation’, :collection =\>  
@ticket.violations,  
:locals =\> { :f =\> f, :valid\_violations =\> Violation.find  
(:all) } %\>

…

\_violation.html.erb:

…

\<%= f.collection\_select :violations, valid\_violations, :id, :name %\>

…

Should I be referencing the citation instead of the violation? I  
cannot figure out where this needs to go. Please help.
