Kuberan
Kuberan

Reputation: 141

How to write a method to get a JSON data and parse it and then save into database

I tried to render the data from my database to_json like this, it works well.

  def getOrderDetails
    #To get the details of a particular guest_order and its batches and items
    @guest_order = GuestOrder.find(params[:id])
    render json: @guest_order.to_json(except: [:created_at, :updated_at], 
            include: {order_batches: {except: [:guest_order_id, :created_at, :updated_at], 
            include: {order_items: {except: [:order_batch_id, :created_at, :updated_at] } }
                }
              }
            )
  end

But, I don't know how to write a method to get a json data and parse it and then update into database.

If I send a json data from mobile like this,

 Parameters:{"updateStatus" => "{\"itemId\":1,\"status\":\"accepted\",\"statusTime\":\"2012-04-25 18:28:30\",\"batchId\":5}"}

Here status is one of these accepted, cooking, ready, delivered, cancelled. statusTime is the value of the particular status.

How can I get this data and parse and then update into the following table in database

def updateStatus

  # How to parse json and save in database

end

The table schema looks like this,

# == Schema Information
#
# Table name: order_items
#
#  id             :integer         not null, primary key
#  quantity       :integer
#  accepted       :datetime
#  cooking        :datetime
#  ready          :datetime
#  delivered      :datetime
#  cancelled      :datetime
#  order_batch_id :integer
#  dish_id        :integer
#  created_at     :datetime
#  updated_at     :datetime
#

Thanks in advance.

Upvotes: 2

Views: 673

Answers (3)

Kuberan
Kuberan

Reputation: 141

  def updateStatus
    order = JSON.parse(params["updateStatus"])
    @item = OrderItem.where(order_batch_id: order["batchId"], item_id: order["itemId"])
    status = order["status"]
    case status
      when "cancelled"
        @item.first.update_attributes(cancelled: order["statusTime"])
      when "accepted"
        @item.first.update_attributes(accepted: order["statusTime"])
      when "cooking"
        @item.first.update_attributes(cooking: order["statusTime"])
      when "ready"
        @item.first.update_attributes(ready: order["statusTime"])
      when "delivered"
        @item.first.update_attributes(delivered: order["statusTime"])
    end
    render json: @item.to_json(except: [:created_at, :updated_at], message: "success")
  end

Upvotes: 0

Vik
Vik

Reputation: 5961

Try :

Install json gem

data = JSON.parse(params[:data])
new_record = OrderItem.new(data[:order_items]) 
new_record.save

Upvotes: 0

Soundar Rathinasamy
Soundar Rathinasamy

Reputation: 6728

Rails will create a hash based the query parameters in the request.

If you give request in correct format the Rails params hash will contain the below data.

puts params.inspect
=>
{ "order_items" => { "id" => 9, "cooking" => "2012-03-11 15:16:15.0000" }, :action => "action_name", etc }

Actually you don't need to parse json at all.

def updateStatus
  @order_item = OrderItem.create(params[:order_items])
  respond_to do |format|
    format.json { render @order_item }
  end
end

Upvotes: 0

Related Questions