-
Notifications
You must be signed in to change notification settings - Fork 0
Description
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.