views:

78

answers:

2

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

+1  A: 
ActiveModel::Base.include_root_in_json = false

in an initializer??

Cody Caughlan
Ah I was editing the question while you wrote this. I had tried this already, and am getting an error. Any ideas?
Mark L
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
A: 

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

  ...
end
neonski