Catch Ruby errors before your users do. Monitor Ruby production errors in real-time and debug them in minutes, before users notice. Our open source Ruby SDK works with Rails, Sinatra, and more. Use this link and get one month free!

RubyFlow The Ruby and Rails community linklog

×

The Ruby and Rails community linklog

Made a library? Written a blog post? Found a useful tutorial? Share it with the Ruby community here or just enjoy what everyone else has found!

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

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in