Search code examples
ruby-on-railssequelfastjsonapi

Using Sequel with jsonapi-serializer


In a Rails api project I added this gem for returning json

gem 'jsonapi-serializer'

These are the models I have defined, all very basic stuff going on.

class UserSequel < Sequel::Model(:users)
end

class User < ActiveRecord::Base
end

I generated a serializer class

class UserSerializer
  include FastJsonapi::ObjectSerializer
  attributes :text
end

Now to the question

# Using ActiveRecord model returns some json as expected
UserSerializer.new(User.first).serializable_hash.to_json

The following however...

# Using Sequel model
UserSerializer.new(UserSequel.first).serializable_hash.to_json

returns this error message

FastJsonapi::MandatoryField (id is a mandatory field in the jsonapi spec)

I don't quite understand why this happens, because obviously the Sequel model contains a column id and calling this returns the expected outcome

UserSequel.first.id 
# => 1

Solution

  • Turned out this was a bug in the jsonapi-serializer gem which has been resolved now. (not released though as of this writing).

    Use the git option to get the latest version

    gem 'jsonapi-serializer', git: 'git://github.com/jsonapi-serializer/jsonapi-serializer'
    

    https://github.com/jsonapi-serializer/jsonapi-serializer/issues/112