---
title: "Errors can be indexed with nested attributes in Rails 5"
description: "We can now opt to index errors on nested attributes in Rails 5"
canonical_url: "https://www.bigbinary.com/blog/errors-can-be-indexed-with-nested-attrbutes-in-rails-5"
markdown_url: "https://www.bigbinary.com/blog/errors-can-be-indexed-with-nested-attrbutes-in-rails-5.md"
---

# Errors can be indexed with nested attributes in Rails 5

We can now opt to index errors on nested attributes in Rails 5

- Author: Abhishek Jain
- Published: July 7, 2016
- Categories: Rails 5, Rails

We use `accepts_nested_attributes_for` when we want a single form to cater to
multiple models. By using this we can easily provide attributes for associated
models.

In Rails 4.x, if a validation fails for one or more of the associated models,
then it is not possible to figure out from error message, which of the
associated model object is the error related to.

```ruby

class Product < ApplicationRecord
has_many :variants
accepts_nested_attributes_for :variants
end

class Variant < ApplicationRecord
validates :display_name, :price, presence: true
end

> > product = Product.new(name: 'Table')
> > variant1 = Variant.new(price: 10)
> > variant2 = Variant.new(display_name: 'Brown')
> > product.variants = [variant1, variant2]
> > product.save
> > => false

> > product.error.messages
> > => {:"variants.display_name"=>["can't be blank"], :"variants.price"=>["can't be blank"]}

```

In the example above we can see that if this error message is sent as JSON API,
we cannot find out which variant save failed because of which attribute.

This works well when we render forms using Active Record models, as errors are
available on individual instances. But, the issue arises with an API call, where
we don't have access to these instances.

## Rails 5 allows indexing of errors on nested attributes

In Rails 5, we can [add an index](https://github.com/rails/rails/pull/19686) to
errors on nested models.

We can add the option `index_errors: true` to `has_many` association to enable
this behavior on individual association.

```ruby

class Product < ApplicationRecord
has_many :variants, index_errors: true
accepts_nested_attributes_for :variants
end

class Variant < ApplicationRecord
validates :display_name, :price, presence: true
end

> > product = Product.new(name: 'Table')
> > variant1 = Variant.new(price: 10)
> > variant2 = Variant.new(display_name: 'Brown')
> > product.variants = [variant1, variant2]
> > product.save
> > => false

> > product.error.messages
> > => {:"variants[0].display_name"=>["can't be blank"], :"variants[1].price"=>["can't be blank"]}

```

## Using global configuration

In order to make this change global, we can set configuration
`config.active_record.index_nested_attribute_errors = true` which is `false` by
default.

```ruby

config.active_record.index_nested_attribute_errors = true

class Product < ApplicationRecord
has_many :variants
accepts_nested_attributes_for :variants
end

class Variant < ApplicationRecord
validates :display_name, :price, presence: true
end

```

This will work exactly same as an example with
`has_many :variants, index_errors: true` in `Product`.

## Links

- [Human page](https://www.bigbinary.com/blog/errors-can-be-indexed-with-nested-attrbutes-in-rails-5)
