1

I'm currently trying to create an Order instance. There is an association of the model Order with Items. The association is as follows. Order has many Items. I try following the documentation https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

class Order < ApplicationRecord
  has_many :items
  accepts_nested_attributes_for :items
end

class OrdersController < ApplicationController
  ##
  private

  def order_params
    params.require(:order).permit(:description,
                                  items_attributes: [:id, :quantity])
  end
end

From the following post, it shows that the id has to be pass in the params. Rails 5 Api create new object from json with nested resource

params = {order: {description: "this is a test"}, items_attributes: [{id: 3, quantity: 3, description: 'within order -> item'}]}
=> {:order=>{:description=>"this is a test"}, :items_attributes=>[{:id=>3, :quantity=>3, :description=>"within order -> item"}]}
[7] pry(main)> order_test = Order.create!(params[:order])
   (0.4ms)  BEGIN
  Order Create (62.9ms)  INSERT INTO "orders" ("description", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["description", "this is a test"], ["created_at", "2019-05-30 23:31:39.409528"], ["updated_at", "2019-05-30 23:31:39.409528"]]
   (4.6ms)  COMMIT
=> #<Order:0x00007ff91556e4b8 id: 14, description: "this is a test", created_at: Thu, 30 May 2019 23:31:39 UTC +00:00, updated_at: Thu, 30 May 2019 23:31:39 UTC +00:00>

I create an order however when I check for the items it returns an empty array.

=> #<Order:0x00007ff9142da590 id: 14, description: "this is a test", created_at: Thu, 30 May 2019 23:31:39 UTC +00:00, updated_at: Thu, 30 May 2019 23:31:39 UTC +00:00>
[11] pry(main)> Order.last.items
  Order Load (0.4ms)  SELECT  "orders".* FROM "orders" ORDER BY "orders"."id" DESC LIMIT $1  [["LIMIT", 1]]
  Item Load (0.3ms)  SELECT "items".* FROM "items" WHERE "items"."order_id" = $1  [["order_id", 14]]
=> []

Here is the table for items:

class CreateItems < ActiveRecord::Migration[5.2]
  def change
    create_table :items do |t|
      t.references :order, foreign_key: true
      t.integer :quantity
      t.string :description

      t.timestamps
    end
  end
end

What is wrong?

1 Answer 1

0

The mistake was in the order the parameters were being passed.

{:order=>{:id=>22, :description=>"this is a test", :items_attributes=>[{:order_id=>22, :quantity=>3, :description=>"within order -> item"}]}}

This solved it:

order = Order.create!(params[:order])

<Order:0x00007ff918039ee0 id: 22, description: "this is a test", created_at: Fri, 31 May 2019 01:39:23 UTC +00:00, updated_at: Fri, 31 May 2019 01:39:23 UTC +00:00>
[39] pry(main)> order.items
  Item Load (114.8ms)  SELECT "items".* FROM "items" WHERE "items"."order_id" = $1  [["order_id", 22]]
=> [#<Item:0x00007ff9180394b8
  id: 4,
  order_id: 22,
  quantity: 3,
  description: "within order -> item",
  created_at: Fri, 31 May 2019 01:39:23 UTC +00:00,
  updated_at: Fri, 31 May 2019 01:39:23 UTC +00:00>]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.