No commit activity in last 3 years
No release in over 3 years
Provides an #includes method to eager load associations of ActiveRecord instances, for when you don't have access to the class object or ActiveRecord::Relation instance.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 3.0.0
 Project Readme

Adds includes method to ActiveRecord instances

Why is this needed?

Sometimes you don't have access to the code that instantiates a particular ActiveRecord instance, so you can't call includes on the class or ActiveRecord::Relation instance to eager load associations.

For example:

# Defines the @post instance variable for us
load_and_authorize_resource

def show
  render @post 
end

Normally this isn't a problem, because if you have a single instance, it's not possible to encounter a N+1 query while working with that instance or any of its direct associations.

%h1= @post.title  

%ul
  - @post.authors.each do |author|
    %li= author.name

The problem occurs with more complex (deeply nested) processing, and you have to crawl over the data of nested has_many associations.

%ul
  - @post.authors.each do |author|
    -# Causes a N+1 query problem:
    %li= "#{author.name} (#{author.nicknames.pluck(:value).join(',')})

That's when you need activerecord-instance_includes:

@post.includes(authors: [:nicknames])

Installation

Add this line to your application's Gemfile:

gem 'activerecord-instance_includes'

And then execute:

$ bundle

Or install it yourself as:

$ gem install activerecord-instance_includes

Usage

Just call includes on ActiveRecord instances in the same way you would normally do so for the ActiveRecord class or ActiveRecord::Relation instance.

The #includes method returns the ActiveRecord instance itself so you can chain method calls:

%ul
  - @post.includes(authors: [:nicknames]).authors.each do |author|
    %li= "#{author.name} (#{author.nicknames.pluck(:value).join(',')})