Skip to content

Possible to specify serializer to use for relationship of different name? #134

@mecampbellsoup

Description

@mecampbellsoup

I have a JobSerializer that has 2 relationships - the hosted file, and outputs.

(This is because a job is rendered from a file that the user uploaded - the "hosted file" - and the result of running the job are output files - "outputs".)

Both the singular hosted file, and the plural output, relationships are modeled by a class called HostedFile (not backed by ActiveRecord, but rather backed by the filesystem).

I want to use the same serializer for both relationships, i.e. API::V1::HostedFileSerializer.

Is it possible to specify the serializer to use for a relationship, e.g.:

class API::V1::JobSerializer < API::V1::BaseSerializer
  include JSONAPI::Serializer

  has_one :hosted_file
  has_many :outputs, serializer: API::V1::HostedFileSerializer

Ideally I could then do something like the following and both relationships would be serialized using the API::V1::HostedFileSerializer:

class JobsController < ApplicationController
  def show
    serialize_model job, include: ['outputs', 'hosted-file']
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions