Notes posted by felixmcallister

RSS feed
September 1, 2008 - (>= v2.1.0)
5 thanks

ActiveRecord::Base.include_root_in_json

From Rails 2.1 onwards, the variable

ActiveRecord::Base.include_root_in_json

affects how the JSON is generated. If this is true (default), then the JSON isn’t like the one above. Instead you’ll get:

konata = User.find(1)
konata.to_json
# => { "user": { "id": 1, "name": "Konata Izumi", "age": 16,
      "created_at": "2006/08/01", "awesome": true}}

(Note the model name is included as a root of the JSON object)

For Rails 2.1 generated projects, you’ll see this in the config/initializers/new_rails_defaults.rb file. You’ll need to set the value to false if you want the old behaviour.

ActiveRecord::Base.include_root_in_json = false