---
title: "Rails 7 adds setting for enumerating columns in select statements"
description:
  "Ruby 7.0 adds setting for enumerating columns in select statements"
canonical_url: "https://www.bigbinary.com/blog/rails-7-adds-setting-for-enumerating-columns-in-select-statements"
markdown_url: "https://www.bigbinary.com/blog/rails-7-adds-setting-for-enumerating-columns-in-select-statements.md"
---

# Rails 7 adds setting for enumerating columns in select statements

Ruby 7.0 adds setting for enumerating columns in select statements

- Author: Ashik Salman
- Published: October 13, 2021
- Categories: Rails, Rails 7

Rails 7 has introduced a new setting called
`enumerate_columns_in_select_statements` for enumerating columns in ActiveRecord
select query statements by which we can avoid common
[ActiveRecord::PreparedStatementCacheExpired](https://apidock.com/rails/ActiveRecord/PreparedStatementCacheExpired)
errors.

Rails uses prepared statements for database query efficiency. When prepared
statements are being used, the repeated queries will be cached based on the
prepared statement query plan at the Postgres database level. This cached value
will become invalid when the returned results are changed.

Whenever we make any schema changes to the database tables while the application
is running, the cached select statements with a wildcard column definition will
raise `PreparedStatementCacheExpired` error since the query output has modified.

### Before

```ruby
=> User.limit(10)
=> SELECT * FROM users LIMIT 10
```

If we use the select query with `*`, then any change in the database schema for
the particular table (eg: users) will invalidate the prepared statement cache
and result in the `PreparedStatementCacheExpired` error. The solution here is to
mention the columns explicitly in the select statement as shown below:

```ruby
=> SELECT "first_name,last_name,email ..." FROM users LIMIT 10
```

### Rails 7 onwards

Rails 7 adds a new setting by which we can ensure all select statements are
generated by enumerating the columns explicitly. Hence, any modifications to the
database schema won't result in `PreparedStatementCacheExpired`, instea,d the
prepared statements will be changed and the respective query will be cached
freshly by the Postgres database.

We can either configure the setting for all models or at a specific model level.

```ruby
# config/application.rb
module MyApp
  class Application < Rails::Application
    config.active_record.enumerate_columns_in_select_statements = true
  end
end

# User model specific
class User < ApplicationRecord
  self.enumerate_columns_in_select_statements = true
end
```

When the setting value is set to `true` the select statement will always
contains columns explicitly.

```ruby
=> User.limit(10)
=> SELECT "first_name,last_name,email ..." FROM users LIMIT 10
```

Check out this [pull request](https://github.com/rails/rails/pull/41718) for
more details.

## Links

- [Human page](https://www.bigbinary.com/blog/rails-7-adds-setting-for-enumerating-columns-in-select-statements)
