ruby-on-railsjsonmongoidfastjsonapi

How to use Netflix fast_jsonapi with Mongoid relationships


How can I add the Mongoid associations for fast_jsonapi?

embeds_many
embeds_one

I'm trying to upgrade from my rabl serialization to fast_jsonapi. Is this even possible?

Mongoid 5.4 fast_jsonapi 1.5


Solution

  • I've followed this guide from #[soundstripe][1]

    [1]: https://medium.com/soundstripe-engineering/greener-pastures-migrating-a-production-api-from-activemodel-serializers-to-fast-json-api-9627be51c64 to figure out how to get fast-jsonapi to work for me. But in general it looks like you just need these pieces of the code to make it work.

    class BookSerializer < ApplicationSerializer
      belongs_to :library
    end
    
    class BooksController < ApplicationController
      def index
        @books = Book.all
        render jsonapi: BookSerializer.new(@books)
      end
    end
    
    class ApplicationSerializer
      include FastJsonapi::ObjectSerializer
    end