views:

26

answers:

1

Hi I have a JSON string that looks like this (Usingt Rails and a REST service)

  {  
   person:   
      {
         name:"Pepe",
         last:"Smith"
         hats:[ { team:"lakers", color:"purple"}, { team:"heats", color:"red" }]   }   }

I want to be able to able to get that JSON, and save the Person to the database, but I want to save the "hats".. as a string to the database; without parsing it or anything like that

i.e. I want to save this to SQL:

hats = "[ { team:"lakers", color:"purple"}, { team:"heats", color:"red" }] }"

Is there a way to do this in rails?

A: 

The JSON string gets converted to params hash before a controller action is invoked. You could make to_json call on the hats attribute to get an equivalent json string.

def create
  params[:person][:hats] = (params[:person][:hats]||{}).to_json
  p = Person.new(params[:person])
  if p.save
    #success
  else
    #error
  end
end
KandadaBoggu