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.
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.
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
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!
@spinlock99 The updated link is https://github.com/rails-api/active_model_serializers/blob/0-10-stable/docs/general/configuration_options.md#default_includes
Most helpful comment
@spinlock99 The updated link is https://github.com/rails-api/active_model_serializers/blob/0-10-stable/docs/general/configuration_options.md#default_includes