includes
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0 (0)
- 3.0.9 (0)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (21)
- 4.1.8 (11)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (0)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (8)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (38)
- 7.1.3.4 (0)
- What's this?
includes(*args)
public
Specify associations args to be eager loaded to prevent N + 1 queries. A separate query is performed for each association, unless a join is required by conditions.
For example:
users = User.includes(:address).limit(5) users.each do |user| user.address.city end # SELECT "users".* FROM "users" LIMIT 5 # SELECT "addresses".* FROM "addresses" WHERE "addresses"."id" IN (1,2,3,4,5)
Instead of loading the 5 addresses with 5 separate queries, all addresses are loaded with a single query.
Loading the associations in a separate query will often result in a performance improvement over a simple join, as a join can result in many rows that contain redundant data and it performs poorly at scale.
You can also specify multiple associations. Each association will result in an additional query:
User.includes(:address, :friends).to_a # SELECT "users".* FROM "users" # SELECT "addresses".* FROM "addresses" WHERE "addresses"."id" IN (1,2,3,4,5) # SELECT "friends".* FROM "friends" WHERE "friends"."user_id" IN (1,2,3,4,5)
Loading nested associations is possible using a Hash:
User.includes(:address, friends: [:address, :followers])
Conditions
If you want to add string conditions to your included models, you’ll have to explicitly reference them. For example:
User.includes(:posts).where('posts.name = ?', 'example').to_a
Will throw an error, but this will work:
User.includes(:posts).where('posts.name = ?', 'example').references(:posts).to_a # SELECT "users"."id" AS t0_r0, ... FROM "users" # LEFT OUTER JOIN "posts" ON "posts"."user_id" = "users"."id" # WHERE "posts"."name" = ? [["name", "example"]]
As the LEFT OUTER JOIN already contains the posts, the second query for the posts is no longer performed.
Note that #includes works with association names while #references needs the actual table name.
If you pass the conditions via a Hash, you don’t need to call #references explicitly, as #where references the tables for you. For example, this will work correctly:
User.includes(:posts).where(posts: { name: 'example' })
NOTE: Conditions affect both sides of an association. For example, the above code will return only users that have a post named “example”, and will only include posts named “example”, even when a matching user has other additional posts.
Links and basic explanation
This function, available on any Base derived class, allows eager loading. So when iterating over a result, all the data can be pre-cached, reducing the number of queries from 1+x to 2.
Several ways to use this include:
Post.includes(:author).where(..)
Post.includes([:author, :comments]).where(..)
Post.includes( :comments => :replies ).where(..)
The result is a Relation, so where and other function may be called.
.includes(:comments).to_a is the same as , .find(:all, :includes => :comments)
Some good documentation here: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
Apperently one can get similar results by using joins, but i’m not expert on joins, so please ammend and read: http://guides.rubyonrails.org/active_record_querying.html