Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i set include_root_in_json to false for all my RoR models?

How can i set include_root_in_json to false for all my RoR models?

I've tried to set ActiveModel::Base.include_root_in_json = false inside application.rb, but it has no effect.

like image 284
Alexey Zakharov Avatar asked Dec 10 '10 10:12

Alexey Zakharov


2 Answers

Model.to_json method is used to returns a JSON string representing the model and i have used self.include_root_in_json = false in a model itself.

But for the whole application you can try setting ActiveModel::Base.include_root_in_json = false in an initializer.[edit This does not work.]

[edit] ActiveRecord::Base.include_root_in_json = true try adding this line to config/environment.rb file.

like image 150
2 revs Avatar answered Sep 21 '22 23:09

2 revs


I'm not sure if this is true for the later versions of rails but for me (running 2.3.5 atm) there was already a declaration for this setting in /config/initializers/new_rails_defaults.rb where it was set to:

ActiveRecord::Base.include_root_in_json = true

So if you try to set this setting in another place then it might be overridden.

like image 23
DanneManne Avatar answered Sep 23 '22 23:09

DanneManne