Active_model_serializers: Serializers with polymorphic association return no attributes for relationship

Created on 2 Sep 2016  路  7Comments  路  Source: rails-api/active_model_serializers

Expected behavior vs actual behavior

When serializing an object with a has_many association, where the association has a polymorphic association, it should infer the serializer for the polymorphic object and return the data, but instead, it's returning nothing.

Steps to reproduce

class Article < ApplicationRecord
  has_many :entities
end

class Entity < ApplicationRecord
  belongs_to :article
  belongs_to :entityable, polymorphic: true
end

class ArticleSerializer < ActiveModel::Serializer
  has_many :entities
end

class EntitySerializer < ActiveModel::Serializer
  has_one :entityable, polymorphic: true
end

If we call the following:

ArticleSerializer.new(@article).to_json

and assuming the article has two entities, we'd expect the following in the JSON:

entities: [{ id: ... }, { id: ... }]

Instead, we get the following:

entities: [{}, {}]

Empty hashes. Interestingly enough, if I do EntitySerializer.new(@entity), it serializes the data correctly, so I'm confident it's not about being unable to infer the serializer based on the object.

Environment

ActiveModelSerializers Version _(commit ref if not on tag)_: 0.10.2

Output of ruby -e "puts RUBY_DESCRIPTION":
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]

OS Type & Version:
OSX 10.11.5

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

All 7 comments

Nevermind, the issue is the default_includes option. This was VERY unclear and should probably be better documented.

we have a default_includes option?

@kris can you trace back how you looked for the documentation and thought process and help use make it easier to find and know about?

oh, that option. I thought you were talking about the per-serializer one everyone keeps wanting. :-\

my bad.

@kris what was the solution? We're having a similar issue and the link you've posted doesn't work anymore.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kapso picture kapso  路  4Comments

yjukaku picture yjukaku  路  5Comments

adamcrown picture adamcrown  路  4Comments

Andriykoo picture Andriykoo  路  5Comments

seoyoochan picture seoyoochan  路  4Comments