ruby - Breaking changes in Rails 4 JSON rendering? -
i started new app atop rails 4 , i've noticed seems breaking change in how json rendering implemented default, can't find written anywhere wondering if can give me insight on (1) whether api has changed , (2) how can obtain behavior need (namely, old behavior).
in particular, i'm seeing that, in rails 3
@answer.as_json
...would return...
{ id: 1, body: "lorem ipsum..." .... }
in rails 4 seems same method returning:
{ 'answer': { id: 1, body: "lorem ipsum..." ... } }
can else confirm behavior has changed? there way old behavior short of overriding as_json every model?
this new implementation making more cumbersome return json responses containing multiple models (which either have done hash merge in "render :json ..." call in controller action or overriding as_json).
there option activerecord::base.include_root_in_json
controls top-level behaviour of as_json
method. name self-explanatory, guess.
as breaking api change: if dig source, can see default value option changed true
in rails 4.0.0.beta1 later reverted false
in rails 4.0.0.rc1.
if want sure json not contain root node, specify include_root_in_json
option in config/initializers/wrap_parameters.rb
:
activesupport.on_load(:active_record) self.include_root_in_json = false end
Comments
Post a Comment