Skip to content
  • Michael Probber's avatar
    21e448b5
    Errors can be indexed with nested attributes · 21e448b5
    Michael Probber authored
    `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]
    21e448b5
    Errors can be indexed with nested attributes
    Michael Probber authored
    `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]
Loading