Coder Social home page Coder Social logo

zeal's Introduction

Zeal: eager loading (but not too eager) for ActiveRecord collections

Sometimes you need to eager load some associations, but it's not convenient to do so at the same time as loading the original records. Zeal is a simple gem that offers a couple of different ways to trigger ActiveRecord's built-in eager loading on an array of already-retrieved records.

For Example

Let's say you want to do the equivalent of this:

User.find(:all, :limit => 10, :include => {:posts => :comments})

If you're reducing code duplication by loading Users in a before filter that's shared between multiple actions, it may not make sense to eager load the same associations for all of those pages. Instead, you can do this:

# before_filters.rb
@users = User.find(:all, :limit => 10)

# users_controller.rb
def users_and_posts_and_stuff
  @users.eager_load(:posts => :comments)
end

def users_and_friends_and_stuff
  @users.eager_load(:friends, :countrymen)
end

You can now avoid both N+1 and unnecessary preloading, while keeping your code as DRY as possible.

Usage

There are two ways to use Zeal: the nicer, more intrusive way, and the more explicit, less intrusive way.

# more_intrusive.rb
class Array
  include Zeal
end

@users.eager_load(:friends, :countrymen)

# less_intrusive.rb
Zeal.eager_load(@users, :friends, :countrymen)

# or alternately
@users.extend(Zeal).eager_load(:friends, :countrymen)

Your choice!

zeal's People

Contributors

rf- avatar ryanlower avatar

Watchers

Winston Tsang avatar  avatar Jonathan Martin avatar  avatar Jarques avatar Alvin Liang avatar Jure Triglav avatar  avatar Jaikishan Jalan avatar  avatar David Judd avatar Alex Nisnevich avatar Jonathan Tsai avatar Philip Bradley avatar James Pettit avatar Vijay Rudraraju avatar kevin avatar Wybo Wiersma avatar  avatar Travis Derouin avatar James Cloos avatar Stuart Liroff avatar Edmund King avatar Ilya Konyukhov avatar Ryan Jordan avatar  avatar Brian Fallon avatar Alexey avatar Ira Kaplan avatar Helen Sparrow avatar Dmitri Akatov avatar Mark Macdonald avatar  avatar  avatar Robert Tucker avatar Conway Anderson avatar Mitchell Vitez avatar Charlie Swanson avatar Nate Sullivan avatar Derek Wu avatar Mitchell Fox avatar Jack Wines avatar Lindsay avatar Xiao Dong avatar  avatar Sambridi Gautam avatar Davis Vu avatar  avatar Michael Mattheakis avatar Niko Rutherford avatar Nicholas (Nick) Meyer avatar Mandy Chan avatar Kai Daniels avatar  avatar Monte avatar  avatar Samuel Henry avatar Ash avatar Michael Mortonson avatar Ying Hao Chan avatar  avatar Kate avatar Dave Peterson avatar I Park avatar  avatar Brynn Gitt avatar Nathan Mannes avatar Helen Qu avatar Aidan Hodge avatar Hannah avatar Natalie Nakamine avatar Dain Kim avatar  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.