Take the 2-minute tour ×

I'm using Rails 3 w/ Mongoid, (so no ActiveRecord). Mongoid uses ActiveModel's "to_json" method, and by default that method includes the root object in the JSON (which I don't want).

I've tried putting this in an initializer:

ActiveModel::Base.include_root_in_json = false

But get the error

uninitialized constant ActiveModel::Base

Any ideas how I can change this? I changed the default directly in the source-code and it worked fine, but obviously I'd like to do it properly.

The variable is defined at the top of this file: Github - activemodel/lib/active_model/serializers/json.rb

From the docs: "The option ActiveModel::Base.include_root_in_json controls the top-level behavior of to_json. It is true by default."

share|improve this question
add comment

4 Answers

up vote 1 down vote accepted
ActiveModel::Base.include_root_in_json = false

in an initializer??

share|improve this answer
Ah I was editing the question while you wrote this. I had tried this already, and am getting an error. Any ideas? –  Mark L Jun 17 '10 at 5:31
I've talked to more people, and confirmed that this is indeed how it should be done. I must have something else in my code messing it up. –  Mark L Jul 24 '10 at 0:02
I've also have the same problem. Have you resolved it? –  Alexey Zakharov Dec 10 '10 at 11:44
add comment

I know this is old, but another way you can do this is by placing this within your application class in application.rb:

# When JSON-encoding a record, don't wrap the attributes in a hash where the
# key is named after the model
config.active_record.include_root_in_json = false
share|improve this answer
The easy way if you need this to apply to all your models. –  rchampourlier May 27 '11 at 9:21
he asked for ActiveModel, not ActiveRecord, thats a big difference –  Christoph Geschwind Jan 19 '12 at 9:32
add comment

You should simply set it on the class that includes the ActiveModel modules:

class Person
  include ActiveModel::Validations
  include ActiveModel::Serializers::JSON
  self.include_root_in_json = false

share|improve this answer
Thanks, this worked really well for me using a neo4j model –  Peter Ehrlich Jan 26 '12 at 4:13
add comment

If you prefer initializers, it's ActiveRecord::Base, not ActiveModel::Base in Rails versions 2.* and 3.1, possibly 3.0. Looked through the source, and in 3.0 beta it was switched to ActiveModel, but back again to ActiveRecord at some point.

ActiveRecord::Base.include_root_in_json = false

Also, in case you're actually trying to use this functionality, in Rails 3.1 the params wrapper is relevant:


Wraps the parameters hash into a nested hash. This will allow clients to submit POST requests without having to specify any root elements.


share|improve this answer
add comment

Your Answer


