Has_many through select box in form


#1

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.