I'm pretty close on this one but caught up on a minor detail. I am trying to update a has_many :through relationship. When I submit the edit form I am unable to extract the appropriate attribute that I want to update. The loop that updates the qty shipped is not updated that field with the correct value. How can I extract only the qty_shipped attribute from the params[:product_shipments] hash?
This is the contents of my params[:product_shipments] hash that the update action is working with
"product_shipments"=> {"82"=>{"qty_shipped"=>"234"},
"83"=>{"qty_shipped"=>"324"},
"84"=>{"qty_shipped"=>"324"}},
"commit"=>"Update Shipment", "id"=>"250"}
Which has all the information I need to update the shipment because the @shipment.product_shipments loop limit the update to only the shipment_id applicable. My problem is that this is the following sql called by update action
ProductShipment Load (0.3ms) SELECT `product_shipments`.* FROM `product_shipments` WHERE `product_shipments`.`shipment_id` = 250
BEGIN
UPDATE `product_shipments` SET `qty_shipped` = 1 WHERE `product_shipments`.`id` = 82
COMMIT
BEGIN
UPDATE `product_shipments` SET `qty_shipped` = 1 WHERE `product_shipments`.`id` = 83
COMMIT
BEGIN
UPDATE `product_shipments` SET `qty_shipped` = 1 WHERE `product_shipments`.`id` = 84
COMMIT
And here is the update action that produces the above sql:
def update
@shipment = Shipment.find(params[:id])
@shipment.update_attributes(params[:shipment])
@shipment.product_shipments.each do |shipment|
shipment.update_attributes(:qty_shipped=> params[:product_shipments])
end
respond_with @shipment, :location => shipments_url
end
Using rbates nested_forms gem is not preferred because I want to figure this out for the purposes of learning how rails works.
<%= hidden_field_tag("product_shipments[][#{product_shipment.id}]") %>
<%= hidden_field_tag("product_shipments[][product_id]", product_shipment.id) %>
<%= text_field_tag "product_shipments[][qty_shipped]", product_shipment.qty_shipped,:class => 'shipment_qty_field'%> <%=@product.product_name %>