Skip to content

Nested includes with polymorphic association throwing error in validate_includes! #422

@mhluska

Description

@mhluska

I have a controller with the following code:

render json: MenuSerializer.new(menus, include: %i[items.sellable.photos])

The serializers look like this:

class MenuSerializer < ApplicationSerializer
  has_many :items
end

class ItemSerializer < ApplicationSerializer
  belongs_to :sellable, polymorphic: true
end

# In this example, Car is a type of sellable.
class CarSerializer < ApplicationSerializer
  has_many :photos
end

class PhotoSerializer < ApplicationSerializer
  attribute :url
end

However, object_serializer throws the following error:

photos is not specified as a relationship on ItemSerializer

The offending line in the library seems to be:

raise ArgumentError, "#{parsed_include} is not specified as a relationship on #{klass.name}" unless relationship_to_include

If I change it to:

return unless relationship_to_include

Everything seems to work fine 🤷‍♂

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions