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 7860165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:10:55+00:00 2026-06-02T22:10:55+00:00

Rendering data from database to_json works well def getOrderDetails #To get the details of

  • 0

Rendering data from database to_json 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, how to save data to database for the same tables. The parameters is

Parameters: {"guestOrder"=>"{\"GuestOrder\"{\"GuestOrderId\":1,
\"orderTime\":\"2012-04-25 18:28:30\",\"notes\":\"spicy\",\"userId\":14,\"tableId\":1,
\"batch\":[{\"items\":[{\"itemId\":1,\"quantity\":4,\"dishId\":1},
{\"itemId\":2,\"quantity\":4,\"dishId\":3},
{\"itemId\":3,\"quantity\":3,\"dishId\":6}],
\"placed\":\"2012-04-25 18:28:30\",\"batchId\":1}],
\"numberOfAdults\":1,\"numberOfChilderns\":3}}"} 

I’ve parsed the parameter value like this,

def guestOrder
    guest_order = JSON.parse(params["guestOrder"])
    # How to store the values from guest_order to database tables
end

    1.9.2p290 :002 > guestOrder = JSON.parse("{\"GuestOrder\"{\"GuestOrderId\":1,
\"orderTime\":\"2012-04-25 18:28:30\",\"notes\":\"spicy\",\"userId\":14,\"tableId\":1,
\"batch\":[{\"items\":[{\"itemId\":1,\"quantity\":4,\"dishId\":1},
{\"itemId\":2,\"quantity\":4,\"dishId\":3},
{\"itemId\":3,\"quantity\":3,\"dishId\":6}],
\"placed\":\"2012-04-25 18:28:30\",\"batchId\":1}],
\"numberOfAdults\":1,\"numberOfChilderns\":3}}")


 => {"GuestOrder"=>{"GuestOrderId"=>1, "orderTime"=>"2012-04-25 18:28:30", "notes"=>"spicy", 
"userId"=>14, "tableId"=>1, "batch"=>[{"items"=>[{"itemId"=>1, "quantity"=>4, "dishId"=>1}, 
{"itemId"=>2, "quantity"=>4, "dishId"=>3}, {"itemId"=>3, "quantity"=>3, "dishId"=>6}], 
"placed"=>"2012-04-25 18:28:30", "batchId"=>1}], "numberOfAdults"=>1, 
"numberOfChilderns"=>3}} 

Here guest_orders has many order_batches, order_batches has many order_items

# == Schema Information
#
# Table name: guest_orders
#
#  GuestOrderId   :integer         not null, primary key
#  adults         :integer
#  children       :integer
#  orderTime      :datetime
#  tableId        :integer
#
# Table name: order_batches
#
#  batchId        :integer         not null, primary key
#  placed         :datetime
#  guest_order_id :integer
#
# Table name: order_items
#
#  itemId         :integer         not null, primary key
#  quantity       :integer
#  dishId         :integer
#  order_batch_id :integer
  • 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-02T22:10:56+00:00Added an answer on June 2, 2026 at 10:10 pm
      def guestOrder
        guest_order = JSON.parse(params["guestOrder"])["GuestOrder"]
        placed_at = Time.zone.parse(guest_order["orderTime"])
        @order = GuestOrder.new(placed: placed_at, guest_table_id: guest_order["tableId"], user_id: guest_order["userId"], 
                adults: guest_order["numberOfAdults"], children: guest_order["numberOfChilderns"], notes: guest_order["notes"])
        guest_order["batch"].each do |batch|
          placed = Time.zone.parse(batch["placed"])
          @batch = OrderBatch.new(placed: placed)
          batch["items"].each do |item| 
            @batch.order_items << OrderItem.new(dish_id: item["dishId"], quantity: item["quantity"]) 
          end 
          @batch.guest_order = @order
          @batch.save 
        end
        if @order.save 
          render json: @order.to_json(only: [:id], message: "success") 
        else 
          render json: @order.errors 
        end  
      end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have html code in database. my model gets assign with correct data from
I have a database that takes in data from a WYWSWIG enabled textarea, which
I have an map app. Somewhere in my code I am rendering data from
I accept data from TinyMCE. I get into difficulties when I copy and paste
I use JavaScript for rendering 20 tables of 100 rows each. The data for
I'm trying to layout an HTML table (it's tabular data) and it is rendering
I have a Webforms, AJAX-enabled web page which, when rendering large amounts of data,
Migrating Data from MySQL server1 to MySQL server2 server1 Ver 14.12 Distrib 5.0.51a, for
I've recently migrated from Trav 0.12 to Redmine 1.4.0. After migrating data from Rrac
Short question: I need to turn a dynamic image pulled from a database into

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.