Active_model_serializers: Unable to use a serializer which is in a folder (i.e. namespace / versioning)

Created on 22 Apr 2016  Â·  7Comments  Â·  Source: rails-api/active_model_serializers

Any help would be fantastic.

Expected behavior vs actual behavior

Expected:
render json: @contacts, each_serializer: ContactsSerializer should work normally

Actual:
Get error message:
superclass must be a Class (Module given)

Steps to reproduce

_(e.g., detailed walkthrough, runnable script, example application)_

controller in folder: app/controllers/api/v1/contacts_controller.rb
serializer in folder: app/serializers/api/v1/contacts_serializer.rb

Code in Controller:

module API::V1
  class ContactsController < ApplicationController
    def index
      @contacts = Contact.order(created_at: :desc).all
      render json: @contacts, each_serializer: ContactsSerializer
    end
    ...
  end
end

Code in serializer:

module API
  module V1
    class ContactsSerializer < ActiveModelSerializers
      attributes :first_name, :last_name, :email, :coffee_preference, :readable_date_of_birth, :age, :favourite
      ...
    end
  end
end

Environment

ActiveModelSerializers Version 0.10:

Output of ruby -e "puts RUBY_DESCRIPTION":
ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-darwin15]

OS Type & Version:
Mac OSX 10.11.3

Integrated application and version _(e.g., Rails, Grape, etc)_:
Rails 4.2.6

Backtrace

_(e.g., provide any applicable backtraces from your application)_

app/serializers/api/v1/contacts_serializer.rb:3:in `<module:V1>'
app/serializers/api/v1/contacts_serializer.rb:2:in `<module:API>'
app/serializers/api/v1/contacts_serializer.rb:1:in `<top (required)>'
activesupport (4.2.6) lib/active_support/dependencies.rb:457:in `load'
...

Question Needs Submitter Response 0.10.x

All 7 comments

Looks like it's related to using different module nesting, which affects Ruby's constant lookup scope.

module API::V1

and

module API
  module V1

and then possibly how Rails autoloading of undefined constants works

have you tried using the absolute namespace? each_serializer: ::API::V1::ContactsSerializer?

This:

class ContactsSerializer < ActiveModelSerializers

should be

class ContactsSerializer < ActiveModel::Serializer

I chose to go with a different gem. Thanks anyway for your responses.

@patrickgordon Thanks, would you mind sharing what you're using?

@bf4 I ended up going with this gem https://github.com/fotinakis/jsonapi-serializers

Cheers!

I'd recommend https://github.com/cerebris/jsonapi-resources if it's not too late

B mobile phone

On Apr 25, 2016, at 5:30 PM, Patrick [email protected] wrote:

@bf4 I ended up going with this gem https://github.com/fotinakis/jsonapi-serializers

Cheers!

—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub

For anyone looking to achieve this in AMS, @bf4 suggestion works fine, to be explicit, the code in the serializer in api/v1/contacts_serializer.rb should be changed to

module Api::V1
    class ContactsSerializer < ActiveModelSerializers
      attributes :first_name, :last_name, :email, :coffee_preference, :readable_date_of_birth, :age, :favourite
      ...
  end
end
Was this page helpful?
0 / 5 - 0 ratings

Related issues

preist picture preist  Â·  4Comments

Tybot204 picture Tybot204  Â·  3Comments

AlexCppns picture AlexCppns  Â·  5Comments

PratheepV picture PratheepV  Â·  4Comments

axsuul picture axsuul  Â·  4Comments