ArLazyPreload is a Ruby gem that brings association lazy load functionality to your Rails applications. There is a number of built-in methods to solve N+1 problem, but sometimes a list of associations to preload is not obvious–this is when you can get most of this gem.
- Simple. The only thing you need to change is to use
- Fast. Take a look at performance benchmark and memory benchmark.
- Perfect fit for GraphQL. Define a list of associations to load at the top-level resolver and let the gem do its job.
- Auto-preload support. If you don’t want to specify the association list, set
Why should I use it?
Lazy loading is super helpful when the list of associations to load is determined dynamically. For instance, in GraphQL this list comes from the API client, and you’ll have to inspect the selection set to find out what associations are going to be used.
This gem uses a different approach: it won’t load anything until the association is called for the first time. When it happens, it loads all the associated records for all records from the initial relation in a single query.