From 21e448b5a5ab4dec6fd129f4eaba971d46a12bb1 Mon Sep 17 00:00:00 2001 From: Michael Probber Date: Tue, 7 Apr 2015 10:42:57 -0400 Subject: Errors can be indexed with nested attributes `has_many` can now take `index_errors: true` as an option. When this is enabled, errors for nested models will be returned alongside an index, as opposed to just the nested model name. This option can also be enabled (or disabled) globally through `ActiveRecord::Base.index_nested_attribute_errors` E.X. ```ruby class Guitar < ActiveRecord::Base has_many :tuning_pegs accepts_nested_attributes_for :tuning_pegs end class TuningPeg < ActiveRecord::Base belongs_to :guitar validates_numericality_of :pitch end ``` - Old style - `guitar.errors["tuning_pegs.pitch"] = ["is not a number"]` - New style (if defined globally, or set in has_many_relationship) - `guitar.errors["tuning_pegs[1].pitch"] = ["is not a number"]` [Michael Probber, Terence Sun] --- guides/source/configuring.md | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'guides') diff --git a/guides/source/configuring.md b/guides/source/configuring.md index aa66376d5d..4e06c8000f 100644 --- a/guides/source/configuring.md +++ b/guides/source/configuring.md @@ -314,6 +314,10 @@ All these configuration options are delegated to the `I18n` library. by a query exceeds the threshold, a warning is logged. This can be used to identify queries which might be causing memory bloat. +* `config.active_record.index_nested_attribute_errors` allows errors for nested + has_many relationships to be displayed with an index as well as the error. + Defaults to false. + The MySQL adapter adds one additional configuration option: * `ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans` controls whether Active Record will consider all `tinyint(1)` columns in a MySQL database to be booleans and is true by default. -- cgit v1.2.3