Home > Software engineering >  How do I include an array of objects as a value in a table with Ruby on Rails?
How do I include an array of objects as a value in a table with Ruby on Rails?

Time:01-09

I have an Order Table that has a key of item_details, I want the value of item_details to be an array of objects? How can I go about doing this? This is what I have so far. The request is being successfully posted but it looks like this? How do I fix it?

Order Controller

class OrdersController < ApplicationController

    skip_before_action :authorized, only: :create

    def index
        orders = Order.all
        if orders
        render json: orders
        else
            render json: {error: "Order Not Found" }, status: :not_found
        end
    end

    def show
        order = Order.find_by(id: params[:id])
        if order
            render json: order
        else
            render json: { error: "Order Not Found" }, status: :not_found
        end
    end

    def create
        order = Order.create(order_params)
        render json: order, status: :created
    end

    def update
        order = Order.find_by(id: params[:id])
        if order
            order.update(order_params)
            render json: order
        else
            render json: { error: "Order Not Found" }, status: :not_found
        end
    end


    private

    def order_params
        params.permit(:customer_id, :order_date,{ item_details: [:product_id, :quantity] })
    end

end

Order Serializer

class OrderSerializer < ActiveModel::Serializer
  attributes :id, :customer_id, :order_date, :total_cost, :item_details

  belongs_to :customer
  has_many :order_details
  has_many :products

  def total_cost
    cost = []
    self.object.order_details.each do |details|
      product = self.object.products.find {|product| product.id == details.product_id}
      cost << product.price * details.quantity
    end
    return cost.sum
  end

end

Schema

  create_table "orders", force: :cascade do |t|
    t.integer "customer_id"
    t.string "order_date"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.text "item_details", default: [], array: true
  end

Post Man Request

{
    "customer_id": 1,
    "order_date": 1,
    "item_details": [
        {
            "product_id": 1,
            "quantity" : 2
        },
        {
            "product_id": 7,
            "quantity" : 1
        }
    ]
}

Postman Return

{
    "id": 2,
    "customer_id": 1,
    "order_date": "1",
    "total_cost": 0,
    "item_details": [
        "{\"product_id\"=>1, \"quantity\"=>2}",
        "{\"product_id\"=>7, \"quantity\"=>1}"
    ],
    "customer": {
        "id": 1,
        "name": "John Smith",
        "phone_number": "(811) 833 2172",
        "email": "[email protected]",
        "address": "334 Jacobs Plaza, West Robtport, IN 26209-0933",
        "username": "john103",
        "password": null
    },
    "order_details": [],
    "products": []
}

Im not sure why the has appears like this "{"product_id"=>1, "quantity"=>2}" It's a string instead of an actual object?

CodePudding user response:

It is because the item_details array is not being coerced into JSON. It is being coerced into a string.

It seems that you are passing in an array of item_details, but storing it as a string:

t.text "item_details", default: [], array: true

So, you probably want to store an array of hashes.

  • Related