Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lazy load for association #359

Closed
zgid123 opened this issue Nov 9, 2023 · 3 comments
Closed

lazy load for association #359

zgid123 opened this issue Nov 9, 2023 · 3 comments

Comments

@zgid123
Copy link

zgid123 commented Nov 9, 2023

Hi,

I wanna skip association if I don't eager load it. The association extractor does not support it at the moment I guess.

I wonder what is the correct way to do the lazy load for blue printer?

Thanks.

@jhollinger
Copy link
Contributor

The feature request I read here is optional associations. Blueprinter already has optional fields via if: and unless:, and it seems reasonable that associations should have the same.

Rails already has a way to detect if associations are eager loaded. So if Blueprinter adds if/unless options for associations, it could be done like this:

class WidgetBlueprinter < Blueprinter::Base
  association :category, if: ->(_field, widget, _opts) {
    widget.association(:category).loaded?
  }
end

@zgid123
Copy link
Author

zgid123 commented Nov 15, 2023

Thanks for your answer. But I think I will monkey patch the extractor because if we use that approach, every association needs the condition

@ritikesh
Copy link
Collaborator

@zgid123 this is outside of the scope of Blueprinter and has been discussed already in #294. Although blueprinter might open up avenues to assist it, it would not support it natively. Pls follow discussions around #341 / #357 for future scopes.

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

No branches or pull requests

3 participants