Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8163069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:56:16+00:00 2026-06-06T18:56:16+00:00

I have a has_many :through form where I can’t get an extra attribute to

  • 0

I have a has_many :through form where I can’t get an extra attribute to post to the database. I’m fouling up the parameter name somewhere. I can get the foreign keys to post but I have another attribute that I’m trying to track in the join table. Keep in mind this is a 100% ajax based form. Here’s what I know

Edit:
After researching similar issues, I understand I’m supposed to build the form attributes, but the code I’ve found doesn’t work for some reason. Here are some resources.

http://railsforum.com/viewtopic.php?id=20203

Rails 3, nested multi-level forms and has_many through

What I don’t understand is that attaching the product_ids is built into rails. Those values post. Why is it hard to attach the quantity_shipped attribute to that array?

Models and Relationships
  Shipment  has_many :products :through => :product_shipments
  Product   has_many :shipments :through => :product_shipments
  ProductShipments belongs_to :shipment,  belongs_to :product

ProductShipments table
  t.integer    :shipment_id  
  t.integer    :product_id
  t.integer    :qty_shipped  <-- This is the Problem Child

This partial is looped through a few times displaying all the products from a certain vendor. It generates an array for the product_ids and another for the product_shipments quantities.

_product_shipments.html.erb.

<div id="product_shipments_container">
<h3>Assign Products to Ship</h3>
<ul class="product_shipments" id="product_shipments">
  <% Product.by_client(@client_id).each do |product| %>
    <%= content_tag_for :li, product, :value => product.id do %>
      <%= hidden_field_tag("shipment[product_ids][]", product.id) %>
      <%= product.product_name %><%= text_field_tag("product_shipments[qty_shipped]")%> <--This is where the issue lies
    <% end %>
  <% end %>
</ul>
</div>

This is the relevant POST data when the form is submitted

"product_ids"=>["1", "3"]}, "product_shipments"=>{"qty_shipped"=>["32", "23"]}

This is sql that is sent to the database

INSERT INTO `product_shipments` (`product_id`, `qty_shipped`, `shipment_id`) 
VALUES (1, NULL, 155)
INSERT INTO `product_shipments` (`product_id`, `qty_shipped`, `shipment_id`)
VALUES (3, NULL, 155)

Here is the action in my controller

def create
 @shipment = Shipment.new(params[:shipment])

 @product_shipments = @shipment.product_shipments.build(params[:product_shipments])

[:qty_shipped]) <- not correct, but it’s what i got so far
if @shipment.save
respond_with @shipment, :location => shipments_url
else
flash[:notice]= “Not saved”
end
end

Here’s the last issue I’m having.

TypeError (can't convert Symbol into Integer):
  app/controllers/shipments_controller.rb:24:in `[]'
  app/controllers/shipments_controller.rb:24:in `create'

GOT IT. After making the changes with the correct answer below. I was able to correct the controller to the following

@product_shipments = @shipment.product_shipments.build(params[:product_shipments])
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:56:18+00:00Added an answer on June 6, 2026 at 6:56 pm

    The simplest solution is that you need to generate an array of hashes that looks like

    :product_shipments=>[{:product_id=>1, :qty_shipped=>32},{:product_id=>3, :qty_shipped=>23}]
    

    instead of two sets of hashes :shipment=>{:product_ids=>[1,3]} and :product_shipments=>[:qty_shipped=>[32,23]].

    To do this, change your view code to

    <%= hidden_field_tag("product_shipments[][product_id]", product.id) %>
    <%= product.product_name %><%= text_field_tag("product_shipments[][qty_shipped]")%>
    

    Then your controller action should work as is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Organization model that has_many users through affiliations. And, in the form
I have gone through tons of the form_for nested resource questions and can't get
How can I generate form fields for a has_many :through association that has additional
So I have a simple post model that has_many tags :through post_tags. This allows
Is it possible to have multiple has_many :through relationships that pass through each other
I have a basic has_many :through relationship that is bi-directional: calendars have many calendar_calendar_events
I have Product model and it has many categories with a has_many :through association
Ok, so I have 2 models with a has_many :through relationship: class Server <
I have a many-to-many relationship like this: A user has_many organizations through affiliations and
I have the following :has_many, :through association for my models: Group Model: has_many :group_users

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.