---
title: "Rails 5 Warning when fetching with Active Record"
description:
  "We can now log warning when Active Record tries to fetch the result set
  greater than configured limit"
canonical_url: "https://www.bigbinary.com/blog/rails-5-adds-option-to-log-warning-when-fetching-big-result-sets"
markdown_url: "https://www.bigbinary.com/blog/rails-5-adds-option-to-log-warning-when-fetching-big-result-sets.md"
---

# Rails 5 Warning when fetching with Active Record

We can now log warning when Active Record tries to fetch the result set greater
than configured limit

- Author: Abhishek Jain
- Published: April 13, 2016
- Categories: Rails 5, Rails

With large data set we can run into memory issue. Here is an example.

```ruby

>> Post.published.count
=> 25000

>> Post.where(published: true).each do |post|
     post.archive!
   end

# Loads 25000 posts in memory

```

## Rails 5 adds warning when loading large data set

To mitigate issue shown above Rails 5
[has added](https://github.com/rails/rails/pull/18846)
`config.active_record.warn_on_records_fetched_greater_than`.

When this configuration is set to an integer value, any query that returns the
number of records greater than the set limit, logs a warning.

```ruby

config.active_record.warn_on_records_fetched_greater_than = 1500

>> Post.where(published: true).each do |post|
     post.archive!
   end

=> Query fetched 25000 Post records: SELECT "posts".* FROM "posts" WHERE "posts"."published" = ? [["published", true]]
   [#<Post id: 1, title: 'Rails', user_id: 1, created_at: "2016-02-11 11:32:32", updated_at: "2016-02-11 11:32:32", published: true>, #<Post id: 2, title: 'Ruby', user_id: 2, created_at: "2016-02-11 11:36:05", updated_at: "2016-02-11 11:36:05", published: true>,....]

```

This helps us find areas where potential problems exist and then we can replace
inefficient queries with better ones.

```ruby

config.active_record.warn_on_records_fetched_greater_than = 1500

>> Post.where(published: true).find_each do |post|
     post.archive!
   end

# No warning is logged
```

## Links

- [Human page](https://www.bigbinary.com/blog/rails-5-adds-option-to-log-warning-when-fetching-big-result-sets)
