has_many through association

I have 3 models: ticket, part, order

ticket.rb has_many :orders has_many :parts, :through => :orders accepts_nested_attributes_for :orders accepts_nested_attributes_for :parts

part.rb has_many :orders has_many :tickets, :through => :orders

order.rb (this table has two columns: ticket_id and part_id) belongs_to :part belongs_to :ticket

Each support ticket might have "parts" attached to it. Let's say a PC technician received a support ticket to replace a hard-drive, the app should be able to add a "part" to the ticket. So I have two drop down menus for parts.

So in my view, the _form.html.erb file for the ticket, I have this (which displays two drop down menus for parts):   <div class="field">     <%= f.label :solution %><br />     <%= f.text_area :solution, :rows => 3 %>   </div>

  <div class="field">     <% f.fields_for :orders do |o| %>       <p>         <%= o.label :part_id, "Part" %><br />         <%= o.collection_select :id, @parts, :id, :name, :prompt => "Select" %>       </p>     <% end %>   </div>

in tickets_controller.rb I have this (which allows me to display two drop down menus for parts): def new   @ticket = Ticket.new   2.times { @ticket.orders.build } end

But when I submit the form, I get this error: Couldn't find Order with ID=1 for Ticket with ID=

Leonel *.* wrote in post #1018279:

... has_many :tickets, :through => :orders

order.rb (this table has two columns: ticket_id and part_id)

If this table really has only those two columns a simple has_and_belongs_to_many relation would be the appropriate way.

T. N. T. wrote in post #1018286:

Leonel *.* wrote in post #1018279:

... has_many :tickets, :through => :orders

order.rb (this table has two columns: ticket_id and part_id)

If this table really has only those two columns a simple has_and_belongs_to_many relation would be the appropriate way.

@T.N.T. True, it could, but once I get the basic functionality I'm going to add additional attributes, such as quantity.

In the Railscasts about nested forms, rbates shows you how to do something similar but it creates new items. For example, create new Survey, and create new questions along with the new survey.

What I need is, new Ticket, attach existing Parts (selected from a drop down menu) to the ticket using the Orders table (ticket_id, part_id)

where does @parts come from?