You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello,
Firstly, thank you for all of your hard work with this gem, it's been great using it.
I ran into an issue when using something other than the id as a primary key. Here's my resource definition:
moduleApiclassMetro < ::Api::Baseself.primary_key=:short_id# Overriding this to use the primary key instead of the id field## When we represent this resource for serialization (create/update), we do so# with this implementation## @return [Hash] Representation of this object as JSONAPI objectdefas_json_api(*)attributes.slice(:id,:type).tapdo |h|
if:id != self.class.primary_keyh[:id]=attributes[self.class.primary_key]endrelationships_for_serialization.tapdo |r|
h[:relationships]=self.class.key_formatter.format_keys(r)unlessr.empty?endh[:attributes]=self.class.key_formatter.format_keys(attributes_for_serialization)endendendend
without overriding as_json_api, the gem was sending the request to the proper url: /metros/:short_id but the payload looked like this: { data: { id: 1, type: 'metros' } } where the id value was the id field instead of the short_id so the id in the data payload did not match the url and the API was returning a 400 response because they identifiers weren't matching.
Overriding the as_json_api method works, but of course it would be better if the support for custom primary keys was baked into the gem.
Thanks.
The text was updated successfully, but these errors were encountered:
Hello,
Firstly, thank you for all of your hard work with this gem, it's been great using it.
I ran into an issue when using something other than the
id
as a primary key. Here's my resource definition:without overriding
as_json_api
, the gem was sending the request to the proper url:/metros/:short_id
but the payload looked like this:{ data: { id: 1, type: 'metros' } }
where theid
value was theid
field instead of theshort_id
so the id in the data payload did not match the url and the API was returning a400
response because they identifiers weren't matching.Overriding the
as_json_api
method works, but of course it would be better if the support for custom primary keys was baked into the gem.Thanks.
The text was updated successfully, but these errors were encountered: