Giter Club home page Giter Club logo

ar_lazy_preload's Introduction

Cult Of Martians Gem Version Build Status Maintainability Coverage Status

ArLazyPreload

Sponsored by Evil Martians

Lazy loading associations for the ActiveRecord models. #includes, #eager_load and #preload are built-in methods to avoid N+1 problem, but sometimes when DB request is made we don't know what associations we are going to need later (for instance when your API allows client to define a list of loaded associations dynamically). The only possible solution for such cases is to load all the associations we might need, but it can be a huge overhead.

This gem allows to set up lazy preloading for associations - it won't load anything until association is called for a first time, but when it happens - it loads all the associated records for all records from the initial relation in a single query.

Installation

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

gem "ar_lazy_preload"

Usage

For example, if we define the following relation

users = User.lazy_preload(:posts).limit(10)

and use it in the following way

users.map(&:first_name)

there will be one query because we've never accessed posts:

SELECT * FROM users LIMIT 10

However, when we try to load posts

users.map(&:posts)

there will be one more request for posts:

SELECT * FROM posts WHERE user_id in (...)

Auto preloading

If you want the gem to be even more lazy - you can configure it to load all the associations lazily without specifying them explicitly. In order 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.

Worried about the performance? Take a look at benchmarks (TASK=bench and TASK=memory)

License

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

ar_lazy_preload's People

Contributors

dmitrytsepelev avatar palkan avatar pikachuexe avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.