How to set ActiveModel::Base.include_root_in_json to false?

Posted by Mark L on Stack Overflow See other posts from Stack Overflow or by Mark L
Published on 2010-06-17T05:22:54Z Indexed on 2010/06/17 5:33 UTC
Read the original article Hit count: 1208

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."

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby-on-rails3