Skip to content

Pass :include to Serializer #1555

Open
Open
@richmolj

Description

@richmolj

Using JSONAPI, I never want to load relationships unless they are also include'd. By default:

class PostSerializer < ActiveModel::Serializer
  has_many :comments
end

Does nothing for me but cause an unnecessary DB hit. Instead, by desired behavior would be:

class PostSerializer < ActiveModel::Serializer
  has_many :comments, if: :comments_included?

  def comments_included?
    self.instance_options[:include].include?(:comments)
  end
end

Unfortunately the include option is passed to the adapter, but unavailable on the serializer.

Can we add include to the options passed to the serializer?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions