1

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.

Severin
  • 8,508
  • 14
  • 68
  • 117
  • I think your issue may be related to the fact that "safety_features" is not the actual attribute name in the database. Can you try the following `Api::Car.create({additional_equipment: {safety_features: ['ABS']}})` or possibly `Api::Car.create({additional_equipment: {safety_features: ['ABS']}.to_json})` – engineersmnky Dec 03 '20 at 20:39
  • @engineersmnky if `safety_features` just holds a string it get's safed fine. The issue is really just related to the value being an array unfortunately. – Severin Dec 03 '20 at 20:56

0 Answers0