1

I'm new to rails, can somebody help me with json format that rails 3.2.2 produce I want to know where and how i need to make changes to affect my json otput. For example when i click http://localhost:3000/customers.json i got output like this

[{"created_at":"2012-03-17T16:10:59Z","id":1,"name":"Jon","phone":"59665","updated_at":"2012-03-17T16:10:59Z"}]

, but i need ==>

[{"customer":{{"created_at":"2012-03-17T16:10:59Z","id":1,"name":"Jon","phone":"59665","updated_at":"2012-03-17T16:10:59Z"}}]

Thanks in advance.

megas
  • 21,401
  • 12
  • 79
  • 130
alexeyb
  • 153
  • 2
  • 10

1 Answers1

4

You'll want to set ActiveRecord::Base.include_root_in_json = true

Try setting that in an initializer (config/initializers), restarting your server. You should see the root object appear in your json.

Jesse Wolgamott
  • 40,197
  • 4
  • 83
  • 109
  • Can anyone think of a way to override the use of the model class name as the root element (i.e. set include root to true but specify a different name). I have an external app using the JSON and would rather not have to change my model class name (or change the JSON after the fact or any similar ugliness). – Mark Fraser Mar 17 '12 at 22:04
  • Answering my own question...apparently as simple as myobj.to_json(:root => "rootname") – Mark Fraser Mar 17 '12 at 22:36