title: Has_many through associated json data sent through postman not getting populated in the database in rails 4
body: <p>I am trying to make a Post request through Postman which send my vendor model attributes and values and vendor_products model attributes.My json data looks like this</p>
  
  <pre><code>  {
     "name": "Raghu",
     "email": "raghu@gmail.com",
     "phone": "9008849042",
       :
       :
     "vendor_products_attributes":
        {
          [{"product_id" : 7, "copies" : 8},
           {"product_id" : 8, "copies" : 10}]
        }
      }
  </code></pre>
  
  <p>Now all my vendor model fields get populated, but vendor_products models are not been populated.I have used a nested form and nested attributes for vendors and vendor_products</p>
  
  <p>My associations are as follows</p>
  
  <p>A vendor -></p>
  
  <pre><code>    has_many :vendor_products
      has_many :products, through: :vendor_products
  </code></pre>
  
  <p>A product -></p>
  
  <pre><code>    has_many :vendor_products
      has_many :vendors, through: :vendor_products
  </code></pre>
  
  <p>A vendor_product</p>
  
  <pre><code>    belongs_to :vendor
      belongs_to :product
  </code></pre>
  
  <p>Vendor.rb</p>
  
  <pre><code> class Vendor &lt; ActiveRecord::Base
    has_many :vendor_products
    has_many :products, through: :vendor_products
    accepts_nested_attributes_for :vendor_products, :products, 
    :allow_destroy =&gt; true
   end
  </code></pre>
  
  <p>My vendors/_form.html.erb</p>
  
  <pre><code>&lt;%= form_for(@vendor) do |f| %&gt;
  &lt;% if @vendor.errors.any? %&gt;
    &lt;div id="error_explanation"&gt;
    &lt;h2&gt;&lt;%= pluralize(@vendor.errors.count, "error") %&gt; prohibited this 
  vendor from being saved:&lt;/h2&gt;
  
    &lt;ul&gt;
      &lt;% @vendor.errors.full_messages.each do |message| %&gt;
    &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt;
  &lt;% end %&gt;
  &lt;/ul&gt;
  &lt;/div&gt;
  &lt;% end %&gt;
  
  &lt;div class="field"&gt;
  &lt;%= f.label :name %&gt;&lt;br&gt;
  &lt;%= f.text_field :name %&gt;
  &lt;/div&gt;
  :
  :
  :
  &lt;%= f.fields_for :vendor_products do |vproducts| %&gt;
  &lt;div class="field"&gt;
  &lt;%= vproducts.label :product %&gt;&lt;br&gt;
  &lt;%= vproducts.select :product_id, 
  options_from_collection_for_select(Product.all, "id", "product_name"),    
  prompt: "Select something" %&gt;
  &lt;div class="field"&gt;
  &lt;%= vproducts.label :copies %&gt;&lt;br&gt;
  &lt;%= vproducts.number_field :copies %&gt;
  &lt;/div&gt;
  &lt;% end %&gt;
  
  &lt;div class="actions"&gt;
  &lt;%= f.submit %&gt;
   &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>My vendors_controller.rb</p>
  
  <pre><code>class VendorsController &lt; ApplicationController
    before_action :set_vendor, only: [:show, :edit, :update, :destroy]
    respond_to :json
  
    def index
      @vendors = Vendor.all.limit(20)
    end
  
  
    def show 
    end 
  
  
    def new 
      @vendor = Vendor.new
      @product = @vendor.products.build
      @vendor.vendor_products.build
    end
  
  
    def edit
    end
  
  
    def create
      @vendor = Vendor.new(vendor_params)      
  
      respond_to do |format|
       if @vendor.save
          format.html { redirect_to @vendor, 
          notice: 'Vendor was successfully created.' }
         format.json { render :show, status: :created, location: @vendor    
          }
       else
         format.html { render :new }
         format.json { render json: @vendor.errors, status: 
          :unprocessable_entity }
       end
     end
    end
  
  
   def update
      respond_to do |format|
      if @vendor.update(vendor_params)
         format.html { redirect_to @vendor, notice: 'Vendor was
      successfully updated.' }
         format.json { render :show, status: :ok, location: @vendor }
      else
        format.html { render :edit }
        format.json { render json: @vendor.errors, status:
                    :unprocessable_entity }
      end
    end
   end
  
  
  
    private
  
    def set_vendor
      @vendor = Vendor.find(params[:id])
    end
  
    def vendor_params
       params.require(:vendor).permit(:name, :email, :phone_no, 
    :addressline1, 
    :addressline2, :landmark,
    :city, :state, :country, :pincode, :latitude, :longitude, :status, 
      product_attributes: [:product_id, :product_name, :price ], 
      vendor_products_attributes: [:vendor_product_id, :vendor_id, 
      :product_id, 
      :copies])
    end
   end
  </code></pre>
  
  <p>routes.rb</p>
  
  <pre><code>  Rails.application.routes.draw do
  
    match '/vendors/create_vendor' =&gt; 'vendors#create' , via: [:post]
  
      resources :vendor_products
      resources :products
      resources :vendors      
    end
  </code></pre>
  
  <p>My server logs</p>
  
  <pre><code>Started POST "/vendors/create_vendor" for 127.0.0.1 at 2016-07-29    
  15:03:43 +0530
  Processing by VendorsController#create as */*
  Parameters: {"name"=&gt;"Ragtyh", "email"=&gt;"mahesh8mesta@gmail.com", 
  "phone_no"=&gt;"9008849042", "addressline1"=&gt;"#7, Varsha Pg", 
  "addressline2"=&gt;"23rd main marenhalli, JP nagar 2nd phase", 
  "landmark"=&gt;"opp mayura sweets", "city"=&gt;"Bangalore", 
  "state"=&gt;"Karnataka", "country"=&gt;"India", "pincode"=&gt;"560078", 
  "latitude"=&gt;12.898773, "longitude"=&gt;77.5764094, "status"=&gt;true, 
  "vendor_products_attributes"=&gt;[{"product_id"=&gt;9, "copies"=&gt;2}, 
  {"product_id"=&gt;7, "copies"=&gt;3}], "vendor"=&gt;{"name"=&gt;"Ragtyh", 
  "email"=&gt;"mahesh8mesta@gmail.com", "phone_no"=&gt;"9008849042", 
  "addressline1"=&gt;"#7, Varsha Pg", "addressline2"=&gt;"23rd main marenhalli, 
  JP nagar 2nd phase", "landmark"=&gt;"opp mayura sweets", 
  "city"=&gt;"Bangalore", "state"=&gt;"Karnataka", "country"=&gt;"India", 
  "latitude"=&gt;12.898773, "longitude"=&gt;77.5764094, "status"=&gt;true, 
  "pincode"=&gt;"560078"}}
   [1m[35m (0.2ms)[0m  BEGIN
   [1m[36mSQL (0.5ms)[0m  [1mINSERT INTO "vendors" ("name", "email", 
  "phone_no", "addressline1", "addressline2", "landmark", "city", "state", 
  "country", "pincode", "latitude", "longitude", "status", "created_at", 
  "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, 
  $13, $14, $15) RETURNING "id"[0m  [["name", "Ragtyh"], ["email", 
  "mahesh8mesta@gmail.com"], ["phone_no", "9008849042"], ["addressline1", 
  "#7, Varsha Pg"], ["addressline2", "23rd main marenhalli, JP nagar 2nd 
  phase"], ["landmark", "opp mayura sweets"], ["city", "Bangalore"], 
  ["state", "Karnataka"], ["country", "India"], ["pincode", "560078"], 
  ["latitude", 12.898773], ["longitude", 77.5764094], ["status", "t"], 
  ["created_at", "2016-07-29 09:33:48.844763"], ["updated_at", "2016-07-29 
  09:33:48.844763"]]
   [1m[35m (0.7ms)[0m  COMMIT
   Redirected to http://localhost:3000/vendors/100025 
   Completed 302 Found in 5592ms (ActiveRecord: 1.4ms)
  </code></pre>
  
  <p>While the raw json data in postman populates the vendor table, the vendor_products are not populating or been created. How can I solve this.Please note that I am a rails newbie</p>