All Projects → DmitryTsepelev → Ar_lazy_preload

DmitryTsepelev / Ar_lazy_preload

Licence: mit
Lazy loading associations for the ActiveRecord models

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Ar lazy preload

Pluck to hash
Extend ActiveRecord pluck to return array of hashes
Stars: ✭ 275 (-2.14%)
Mutual labels:  activerecord, rails
Jsonapi Utils
Build JSON API-compliant APIs on Rails with no (or less) learning curve.
Stars: ✭ 191 (-32.03%)
Mutual labels:  activerecord, rails
Graphql Query Resolver
Minimize N+1 queries generated by GraphQL and ActiveRecord
Stars: ✭ 148 (-47.33%)
Mutual labels:  activerecord, rails
Closure tree
Easily and efficiently make your ActiveRecord models support hierarchies
Stars: ✭ 1,665 (+492.53%)
Mutual labels:  activerecord, rails
Seamless database pool
Add support for master/slave database clusters in ActiveRecord to improve performance.
Stars: ✭ 222 (-21%)
Mutual labels:  activerecord, rails
Activerecord where assoc
Make ActiveRecord do conditions on your associations
Stars: ✭ 126 (-55.16%)
Mutual labels:  activerecord, rails
Public activity
Easy activity tracking for models - similar to Github's Public Activity
Stars: ✭ 2,822 (+904.27%)
Mutual labels:  activerecord, rails
Active record Events
Manage timestamps in ActiveRecord models
Stars: ✭ 109 (-61.21%)
Mutual labels:  activerecord, rails
Elasticsearch Rails
Elasticsearch integrations for ActiveModel/Record and Ruby on Rails
Stars: ✭ 2,896 (+930.6%)
Mutual labels:  activerecord, rails
Secondbase
Seamless second database integration for Rails.
Stars: ✭ 216 (-23.13%)
Mutual labels:  activerecord, rails
Calculate All
calculate_all method for aggregate functions in Active Record
Stars: ✭ 118 (-58.01%)
Mutual labels:  activerecord, rails
Scenic
Scenic is maintained by Derek Prior, Caleb Hearth, and you, our contributors.
Stars: ✭ 2,856 (+916.37%)
Mutual labels:  activerecord, rails
Where Or
Where or function backport from Rails 5 for Rails 4.2
Stars: ✭ 116 (-58.72%)
Mutual labels:  activerecord, rails
Torque Postgresql
Add support to complex resources of PostgreSQL, like data types, array associations, and auxiliary statements (CTE)
Stars: ✭ 130 (-53.74%)
Mutual labels:  activerecord, rails
Active hash relation
ActiveHash Relation: Simple gem that allows you to run multiple ActiveRecord::Relation using hash. Perfect for APIs.
Stars: ✭ 115 (-59.07%)
Mutual labels:  activerecord, rails
Ooor
Odoo Ruby JSON client. Emulates ActiveRecord enough (as much as Mongoid; Implements ActiveModel) to make Rails development with an Odoo datastore straightforward
Stars: ✭ 184 (-34.52%)
Mutual labels:  activerecord, rails
Counter culture
Turbo-charged counter caches for your Rails app.
Stars: ✭ 1,397 (+397.15%)
Mutual labels:  activerecord, rails
Rails
Ruby on Rails
Stars: ✭ 49,693 (+17584.34%)
Mutual labels:  activerecord, rails
Activerecord Turntable
ActiveRecord Sharding Plugin
Stars: ✭ 206 (-26.69%)
Mutual labels:  activerecord, rails
Activerecord Postgres enum
Integrate PostgreSQL's enum data type into ActiveRecord's schema and migrations.
Stars: ✭ 227 (-19.22%)
Mutual labels:  activerecord, rails

ArLazyPreload Cult Of Martians Gem Version Build Status Maintainability Coverage Status

ArLazyPreload is a 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 #lazy_preload instead of #includes, #eager_load or #preload
  • Fast. Take a look at benchmarks (TASK=bench and TASK=memory)
  • 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 ArLazyPreload.config.auto_preload to true

Sponsored by Evil Martians

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 a first time. When it happens–it loads all the associated records for all records from the initial relation in a single query.

Usage

Let's try #lazy_preload in action! The following code will perform a single SQL request (because we've never accessed posts):

users = User.lazy_preload(:posts).limit(10)  # => SELECT * FROM users LIMIT 10
users.map(&:first_name)

However, when we try to load posts, there will be one more request for posts:

users.map(&:posts) # => SELECT * FROM posts WHERE user_id in (...)

Auto preloading

If you want the gem to be even lazier–you can configure it to load all the associations lazily without specifying them explicitly. To do that you'll need to change the configuration in the following way:

ArLazyPreload.config.auto_preload = true

After that there is no need to call #lazy_preload on the association, everything would be loaded lazily.

If you want to turn automatic preload off for a specific record, you can call .skip_preload before any associations method:

users.first.skip_preload.posts # => SELECT * FROM posts WHERE user_id = ?

Relation auto preloading

Another alternative for auto preloading is using relation #preload_associations_lazily method

posts = User.preload_associations_lazily.flat_map(&:posts)
# => SELECT * FROM users LIMIT 10
# => SELECT * FROM posts WHERE user_id in (...)

Gotchas

  1. Lazy preloading does not work for ActiveRecord < 6 when .includes is called earlier:
Post.includes(:user).preload_associations_lazily.each do |p|
  p.user.comments.load
end
  1. When #size is called on association (e.g., User.lazy_preload(:posts).map { |u| u.posts.size }), lazy preloading won't happen, because #size method performs SELECT COUNT() database request instead of loading the association when association haven't been loaded yet (here is the issue, and here is the explanation article about size, length and count).

Installation

Add this line to your application's Gemfile, and you're all set:

gem "ar_lazy_preload"

License

The gem is available as open source under the terms of the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].