Association belongs_to inside serializer is not returning anything

0

The application contains a module called Dispute , this module contains Procedures , each Procedure contains a user and belongs to a dispute.

As described below, belongs_to is not displayed and the :user attribute is called to return the user (but without the serializer application).

class Dispute::ProceduresSerializer < ActiveModel::Serializer
  attributes :id, :user

  # FIXME: Not working with belongs_to. Called attribute instead.
  # belongs_to :user, serializer: Dispute::UserSerializer
end

class DisputeSerializer < ActiveModel::Serializer
  attributes :id

  has_many :procedures, serializer: Dispute::ProceduresSerializer
end

Below I describe the associations:

class Dispute::Procedure < ApplicationRecord
  belongs_to :dispute
  belongs_to :user
end

class Dispute < ApplicationRecord
  ...
  has_many :procedures, dependent: :destroy
  ...
end

class User < ApplicationRecord
  ...
  has_many :procedures, dependent: :destroy
  ...
end

Looking at the above code, do you have any idea where the problem is?

    
asked by anonymous 02.01.2017 / 22:06

1 answer

0

The problem is that by default, active_model_serializers only displays one level in the serializer. You must add to the file active_model_serializers.rb in config/initializers setting to more than 1 level (each * is a level):

ActiveModel::Serializer.config.default_includes = '**'

This setting has been added in this pull request .

    
03.01.2017 / 13:06