belongs_to association inside serializer is not returning anything

Asked

Viewed 35 times

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 attribute :user is called to return the user (but without the application of the serializer).

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 have any idea where the problem is?

1 answer

0


The problem is that by default the active_model_serializers only presents a level in the serializer. It is necessary to add to the file active_model_serializers.rb in config/initializers setting for more than 1 level (each * is a level):

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

This configuration has been added in this pull request.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.