1

I'm using the gem active_model_serializers and I'm facing some issues with versioning.

Controllers

In app/controllers/v1/contracts_controller.rb

module V1
    class ContractsController < ApiController

        def index
            @contracts = Contract.all
            render json: @contracts
        end

    end
end

In app/controllers/v2/contracts_controller.rb

module V2
    class ContractsController < ApiController

        def index
            @contracts = Contract.all
            render json: @contracts
        end

    end
end

Serializers

In app/serializers/v1/contract_serializer.rb

class ContractSerializer < ActiveModel::Serializer
    attributes :id
end

In app/serializers/v2/contract_serializer.rb

class ContractSerializer < ActiveModel::Serializer
    attributes :id, :name
end

Whether I call the route /v1/contracts or /v2/contracts, the rendered json include the contract name, which means that the serializer in v2 seems to be always called.

FYI, I added config.autoload_paths += Dir[Rails.root.join('app', 'serializers', '**/')] in config/application.rb

Jérôme Boé
  • 2,752
  • 4
  • 21
  • 32

1 Answers1

0

You need to specify the serializers in your controllers, e.g. my answer here

Community
  • 1
  • 1
phaedryx
  • 2,024
  • 3
  • 16
  • 24