Skip to content

Conversation

@JuliaKovtun
Copy link
Contributor

@JuliaKovtun JuliaKovtun commented Oct 9, 2025

This feature enables users to request extra fields for records returned by a query, including both the main results and any records loaded via sideloads. The extra fields are dynamically preloaded and assigned to each record.

How it works:

  1. User Request:

    • The user specifies [extra_fields] in the query, mapping resource types (e.g., "authors") to extra field names (e.g., [:posts_number]).
  2. Preloading Logic:

    • For each requested extra field, the code checks if the associated resource supports it and if a corresponding preload method exists.
    • It collects all records of the target resource type from both the main results and any sideloaded associations.
    • It calls the preload method (e.g., preload_posts_number) on the resource’s model, passing the IDs of all relevant records.
    • The preload method should return a mapping of record IDs to extra field values.
  3. Assignment:

    • The code assigns the extra field value to each record using a setter (e.g., record.posts_number = value).

Example Use Case:

If a user requests posts_number for authors, the feature will:

  • Find all author records in the results and sideloads,
  • Call Author.preload_posts_number(author_ids),
  • Assign the returned value to each author’s posts_number attribute.

Limitations (Current Implementation):
This feature does not support preloading for deep sideloads such as posts.comment.author*. Deeply sideloaded authors (e.g., those reached via a chain like posts → comment → author*) will not appear in the array of relevant records for preload, and thus will not have extra fields assigned. Only records directly reachable via the sideload tree are supported for extra field preloading.

has_many :in, :comments, origin: :post
end

class Comment
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initially when I introduced this, there were small number of classes & lines of code. So I didn't segregate them.

Note for the next time we work on this: we should make it modular by putting every definition of factory & model class in its own file.

@mrhardikjoshi mrhardikjoshi merged commit a0dfc8f into mrhardikjoshi:master Oct 13, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants