ruby on rails - Collection is serializing with the each-object key being the pluralized key, cannot figure out why -
We have found a simple API endpoint that works like this:
DIF Index: Json: country.all end
This unfortunately gives us this output:
{"countries" => [[0] {"Country" = & gt; {"Id" = & gt; 1, "iso" = & gt; "US", "Iso 3" = & gt; "USA", "iso_name" = & gt; "United States", "name" = & gt; "United States", "Nodecode" = & gt; 840}}, [1] {"Country" = & gt; {"Id" = & gt; 2, "iso" = & gt; "Ca.", "iso3" = & gt; "CAN", "iso_name" = & gt; "Canada", "Name" = & gt; "Canada", "Nodecode" = & gt; 124}}]}
Note that the key for each individual object is the form of the plural key
However, when we end the point
DIF INDRED RENDER JASON: {country: country.all} and
The output looks like this:
/ P> {"countries" => [[0] {"Country" = & gt; {"Id" = & gt; 1, "iso" = & gt; "US", "Iso 3" = & gt; "USA", "iso_name" = & gt; "United States", "name" = & gt; "United States", "Nodecode" = & gt; 840}}, [1] {"Country" = & gt; {"Id" = & gt; 2, "iso" = & gt; "Ca.", "iso3" = & gt; "CAN", "iso_name" = & gt; "Canada", "Name" = & gt; "Canada", "Nodecode" = & gt; 124}}]}
Ie, right.
However, setting a key such as {countries: country.all}
is a bad form, and I would like to understand why it is sorted with the collection key instead of the key to each object (That is, why it is plural and not singular for each country
).
We have not overridden to_json
or any other serialization methods We are using the default rail model serial (I tried to create a clear serializer, but there is no change in behavior I can not, for my life, find out why these keys are plugging
edit: There is even more oddity here I have a clear serializer Talk about Was when I set a Siriylijhr (only with the properties normally display), so I get it:
{ "countries" = & gt; [[0] {"id" = & gt; 1, "iso" = & gt; "US", "Iso 3" = & gt; "USA", "iso_name" = & gt; "United States", "name" = & gt; "United States", "Nodecode" = & gt; 840}, [1] {"Country" = & gt; {"Id" = & gt; 2, "iso" = & gt; "Ca.", "iso3" = & gt; "CAN", "iso_name" = & gt; "Canada", "Name" = & gt; "Canada", "Nodecode" = & gt; 124}}]}
The first object has no key , and every other is plural. I tested both in the test, and confirmed to make the actual API call.
I think an override & lt; = & Gt; Apart from
and to_s
, nothing override it can be found. Should not this affect the output in this manner?
Mike, I think you have copied and pasted some wrong, or you might just be confused .
The output of both outputs is the same: countries (plural) as main feedback, and country (single) for every country or I am saying wrong?
Comments
Post a Comment