Sitelet https://github.com/jsonapi-serializer/jsonapi-serializer/issues/183
Skip to content

Combining models of different types in a collection #183

Description

@jamesdixon

Hi there,

Is there a preferred method to combine multiple models of different types in a collection?

I have two models, Inspection and Event, and need to return those as a single collection. I was expecting that when calling render jsonapi: models, that it would automatically pick the proper serializer based on the type of the model but it blows up.

Currently, I'm manually handling the process with this but it feels messy:

    inspections = load_inspections
    events = load_events
    serialized_inspections = Api::V1::Mobile::InspectionSerializer.new(inspections).serializable_hash
    serialized_events = Api::V1::Mobile::EventSerializer.new(events).serializable_hash

    eventables = { data: serialized_events[:data] + serialized_inspections[:data] }

    render json: eventables.to_json, content_type: 'application/vnd.api+json'

In a perfect world, I'd love to be able to do something like:

    inspections = load_inspections
    events = load_events
    
    render jsonapi: inspections + events

Thanks and apologies in advance if I'm missing something obvious!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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