---
title: "Rails 5.1 doesn't load all records on calling Model.all#inspect"
description:
  "Rails 5.1 limits number of records while calling inspect on
  ActiveRecord::Relation object."
canonical_url: "https://www.bigbinary.com/blog/do-no-load-all-records-on-activerecord-relation-inspect"
markdown_url: "https://www.bigbinary.com/blog/do-no-load-all-records-on-activerecord-relation-inspect.md"
---

# Rails 5.1 doesn't load all records on calling Model.all#inspect

Rails 5.1 limits number of records while calling inspect on
ActiveRecord::Relation object.

- Author: Mohit Natoo
- Published: November 14, 2017
- Categories: Rails 5.1, Rails

Let's take a project with hundreds of users. When we call inspect on `User.all`,
we see an array of 10 users followed by `...`. That means the output of
`#inspect` method shows data only for 10 records.

```ruby
> User.all.inspect
User Load (3.7ms)  SELECT  "users".* FROM "users"
=> "#<ActiveRecord::Relation [
#<User id: 1, email: \"dirbee@example.com\" >,
#<User id: 2, email: \"tee@example.com\">,
#<User id: 3, email: \"scott@example.com\">,
#<User id: 4, email: \"mark@example.com\">,
#<User id: 5, email: \"ben@example.com\">,
#<User id: 6, email: \"tina@example.com\">,
#<User id: 7, email: \"tyler@example.com\">,
#<User id: 8, email: \"peter@example.com\">,
#<User id: 9, email: \"rutul@example.com\">,
#<User id: 10, email:\"michael@example.com\">,
...]>"
```

We can see that the query executed in the process is fetching all the records
even though the output doesn't need all of them.

In Rails 5.1,
[only the needed records are loaded](https://github.com/rails/rails/pull/28592)
when `inspect` is called on ActiveRecord::Relation.

```ruby
> User.all.inspect
User Load (3.7ms)  SELECT  "users".* FROM "users" LIMIT $1 /*application:Ace Invoice*/  [["LIMIT", 11]]

=> "#<ActiveRecord::Relation [
#<User id: 1, email: \"dirbee@example.com\" >,
#<User id: 2, email: \"tee@example.com\">,
#<User id: 3, email: \"scott@example.com\">,
#<User id: 4, email: \"mark@example.com\">,
#<User id: 5, email: \"ben@example.com\">,
#<User id: 6, email: \"tina@example.com\">,
#<User id: 7, email: \"tyler@example.com\">,
#<User id: 8, email: \"peter@example.com\">,
#<User id: 9, email: \"rutul@example.com\">,
#<User id: 10, email:\"michael@example.com\">,
...]>"
```

We can see in the above case that query executed has limit constraint and hence
only the required number of records are loaded.

## Links

- [Human page](https://www.bigbinary.com/blog/do-no-load-all-records-on-activerecord-relation-inspect)
