Lazy preloading for ActiveRecord
https://github.com/DmitryTsepelev/ar_lazy_preload
ArLazyPreload
gem allows you to avoid N+1 problem for cases, when you don’t know the exact list of associations you’re going to load (hi, GraphQL!). When you specify a list of lazy loaded associations, they would be loaded only when accessed for the first time:
users = User.lazy_preload(:posts).limit(10) # => SELECT * FROM users LIMIT 10
users.map(&:posts) # => SELECT * FROM posts WHERE user_id in (...)
Also, if you’re tired of typing lazy_preload each time you can just use ArLazyPreload.config.auto_preload = true
Post a comment