Forum: Ruby on Rails has_many through select box in form

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
cjharrelson (Guest)
on 2008-11-19 20:15
(Received via mailing list)
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.
This topic is locked and can not be replied to.