Parsing array from JSON response body with JSONAPI::Resource in Rails

448 views Asked by At

So I got two Rails 6 applications:

  • Backend (API only)

    • with gem 'jsonapi-resources', '~> 0.9'
    • with gem 'jsonb_accessor', '~> 1.0'
  • Frontend

    • with gem 'json_api_client', '~> 1.18'

In the Backend part I setup a Model (Car) which has a jsonb field to hold attributes (such as equipment). The relevant part of the Model looks like this:

# Backend
class Car < ApplicationRecord
  # additional_equipment is the name of the field in the DB which holds the JSONB
  jsonb_accessor :additional_equipment,
    satefy_features: [:string, array: true, default: []]
end

Then, in the frontend I setup the connection to this Model like so:

# Frontend
module Api
  class Car < JsonApiClient::Resource
    property :safety_features, type: :string, array: true, default: []
  end
end

When I then do a create call via this connection, all values which are present in the request inside the array disappear. So doing this:

Api::Car.create({safety_features: ['ABS']})

will end up in a new Car record having

Car.last.safety_features #=> []

This only happens for the values inside arrays. All other values (strings, integers, etc.) get transferred correctly.

I got the feeling that I am simply missing a configuration somewhere. Any help is much appreciated.

0

There are 0 answers