I've got a big problem. I've got some code doing a REST API, outputting
JSON, that works fine and hasn't been touched in like 9 months. I've
also got a client out there in the wild that I can't touch, so I can't
muck with the API at all.


However in a recent bugfix update I"ve discovered that Rails has changed
a great deal under me, deprecated a lot of stuff, and I'm needing to
bring the code in line with modern expectations. That's fine. Where I'm
stuck is my json rendering all changed.


Say that I've got an array of Scholarship objects with id, name, etc...
When I asked to render the json of an array of scholarships previously I
would get:

[{"scholarship":{"id":2, "name":"foo"}}, {"scholarship":{"id":3,
"name":"bar"...

in short, an array of key value pairs where I had to iterate over the
array and start by saying x["scholarship"] to get at the data.

After update what I'm getting is:

[{"id":2, "name":"foo"}, {"id":3, "name":"bar"...

no root elements. Using the active record serializers I've gotten some
flexibility in specifying the root element, but now I either end up with
the above (no root element) or:

{"scholarships":["scholarships":{"id", 2....

where I get a root element for the whole Array that is then added to
every element.

Is anybody still with me and can help me unwind this? When rendering
the json for a scholarhip I need to specify the "scholarship" root tag
... and when rendering a collection of Scholarships I need to be able to
override and NOT show a root tag?

As noted I can't change the client. so if my resulting JSON is not
identical to what it's always been I've got a real problem!

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c29822439418a4f16551aaefd2703b0f%40ruby-forum.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Na Su at Jan 31, 2014 at 10:12 pm
    Is this what you're looking for?

    selected_objects.map do |obj| {"scholarship" => obj} end.to_json

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/df6bc70baca4f7610fcfa70c088d0621%40ruby-forum.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Frederick Cheung at Feb 1, 2014 at 7:35 pm

    On Thursday, January 30, 2014 9:49:41 PM UTC, Ruby-Forum.com User wrote:

    [{"id":2, "name":"foo"}, {"id":3, "name":"bar"...

    no root elements. Using the active record serializers I've gotten some
    flexibility in specifying the root element, but now I either end up with
    the above (no root element) or:

    {"scholarships":["scholarships":{"id", 2....

    where I get a root element for the whole Array that is then added to
    every element.

    Is anybody still with me and can help me unwind this? When rendering
    the json for a scholarhip I need to specify the "scholarship" root tag
    ... and when rendering a collection of Scholarships I need to be able to
    override and NOT show a root tag?
    Have you tried playing with the include_root_in_json setting?

    If it comes to it you could always override as_json in your model or use
    something like jbuilder to construct your json explicitly

    Fred

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4cd71ec8-40bc-4414-b6b7-7628927e3957%40googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Walter Lee Davis at Feb 1, 2014 at 7:52 pm

    On Feb 1, 2014, at 2:35 PM, Frederick Cheung wrote:



    On Thursday, January 30, 2014 9:49:41 PM UTC, Ruby-Forum.com User wrote:

    [{"id":2, "name":"foo"}, {"id":3, "name":"bar"...

    no root elements. Using the active record serializers I've gotten some
    flexibility in specifying the root element, but now I either end up with
    the above (no root element) or:

    {"scholarships":["scholarships":{"id", 2....

    where I get a root element for the whole Array that is then added to
    every element.

    Is anybody still with me and can help me unwind this? When rendering
    the json for a scholarhip I need to specify the "scholarship" root tag
    ... and when rendering a collection of Scholarships I need to be able to
    override and NOT show a root tag?


    Have you tried playing with the include_root_in_json setting?

    If it comes to it you could always override as_json in your model or use something like jbuilder to construct your json explicitly
    +100 for jbuilder -- using it today for the second time, and discovered that I quite like it. If you lean back and try to do less, it works better!

    Walter
    Fred

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4cd71ec8-40bc-4414-b6b7-7628927e3957%40googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3E6CECD4-D153-4C16-B3B4-D0B3D2F19B84%40wdstudio.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJan 30, '14 at 9:50p
activeFeb 1, '14 at 7:52p
posts4
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2022 Grokbase