2

I have a Kitchen model and I want to add menu list called special_kitchen_menus into it.

I can update the kitchen. But, I get this when I console.log() the result from database:

[{"name"=>"pizza", "price"=>29}, {"name"=>"burger", "price"=>24}, {"name"=>"cake", "price"=>39}]

Line of code that do the console.log()

var a = "<%= @kitchen.special_kitchen_menus %>";
console.log(a);

I try to use serialize and create my own method called convert_to_hash. But I got no luck at all.

schema.rb

create_table "kitchens", force: :cascade do |t|
    # other columns
    t.json     "special_kitchen_menus",  default: {}
end

kitchen model

class Kitchen < ActiveRecord::Base
    # serialize :special_kitchen_menus

    # def convert_to_hash
    #   self.special_kitchen_menus = self.special_kitchen_menus.to_h
    # end
end

kitchen controller

# before_action :convert_to_hash, only: [:update]

def update
    @kitchen = Kitchen.find(params[:id])
    if @kitchen.update(kitchen_params)
      redirect_to @kitchen
    else
      render :edit
    end
end

def kitchen_params
    params.require(:kitchen).permit(:special_kitchen_menus)
end

form

<%= form_for @kitchen do |f| %>
    <!-- other fields -->

    <label>Special Kitchen Menus</label>
    <%= f.text_field :special_kitchen_menus %>              
    <%= f.submit "Update" %>
<% end %>

sample data for update

[
  { "name": "pizza", "price": 29 }, 
  { "name": "burger", "price": 24 }, 
  { "name": "cake", "price": 39 }
]

What should I do to update the input data correctly so that I can retrieve it back correctly just the input data without that weird characters?

Note 1: I don't want to store the data in another model OR use nested attribute. I just want to store the data inside this Kitchen model using the same Kitchen's form.

Note 2: I'm using postgres database. Not sqlite for this app.

2
  • Can you show us the line of codes the has the console.log statement? Commented Oct 16, 2015 at 3:39
  • Just update my question. Thanks for the help! Commented Oct 16, 2015 at 3:41

2 Answers 2

2

Try this:

var a = <%= @kitchen.special_kitchen_menus.to_json.html_safe %>;
console.log(a);
Sign up to request clarification or add additional context in comments.

Comments

2

I don't think you can save JSON array in JSON type column. In the JSON type column, you can only store VALID JSON object. As, array of JSON is not a JSON object, so you should not be able to store that in a json column in Rails. Postgres has an array column type. You should use that to store array of JSON objects instead.

Update

You can use jsonb column type to store array of JSON objects in the database. Take a look at this stackoverflow answer pointed by @brg in the comment section.

4 Comments

Could you please suggest what should I do now?
are you on postgres or mysql?
postgres has array column type that you can use: postgresql.org/docs/9.1/static/arrays.html
You can save JSON ARRAY in jsonb type column. see: stackoverflow.com/questions/36103968/… and stackoverflow.com/questions/38679190/…

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.