Skip to content

Include serializer's has_* relationships #6

@JureCindro

Description

@JureCindro

Problem

fast_jsonapi gem, and others probably as well, need to query for any has_[one|many] relationships to display the ids in the relationships part of the response. This naturally causes N+1 queries in index actions, even for relationships that are not included by the includes query parameter. Including something just makes this problem 1 level deeper, because then the N+1 queries happen for the included relationship has_[one|many] relationships.

Proposed Solution

Improve the Jsonapi::Mixins::Include so that it attempts to look at the included relationship serializer for all the has_* relationships and includes those too, but we have to realise this is a band-aid solution at best.

So having the following serializers, the current implementation of Include mixin recursively includes the included relationships.

class AuthorSerializer
  # truncated for brevity
  has_many :books
  # truncated for brevity
end

class BookSerializer
  # truncated for brevity
  belongs_to :author
  has_many :versions
  # truncated for brevity
end

# GET /books?include=author.books 
collection.includes([{author: :books}])`

This would be improved to:

# GET /books?include=author.books
collection.includes(author: { books: [:versions] })

I think the includes supports this kind of nesting but would need to double-check.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions