Skip to content

Include option for associations in serializers #1333

Open
@oyeanuj

Description

@oyeanuj

I am migrating from 0.9.3 to 0.10.0-rc3 to take advantage of json-api. Based on the current docs, it seems if I want the association's data as part of the response, I will have to specify it as part of 'include' option when calling render in the controller.

Is there a way to specify this in the serializer instead of the controller, so that I can specify it once and everytime I render that serializer, those associations are included in the json-api by default (instead of having to specify it every time I render that object)? That seemed to be the default behavior in the 0.9.x

TLDR: Instead of

# posts_controller.rb
render @post, include: ['author', 'metrics', 'media']

something like this:

# post_serializer.rb

has_many :metrics #or included by default which would be ideal
has_many :media, only: [title]
has_many :publishers, except: [publisher_bio]

# or even
has_one :author, include: true 

Maybe it is already there and I missed it. Either way, I would love some clarity.

Thanks!

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