Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Different nested validations should not be ignored

#3638

This is my first ticket, be nice please.

Given the following relationship.

class Store < ActiveRecord::Base
  has_many :products
  accepts_nested_attributes_for :products
end

class Product < ActiveRecord::Base
  belongs_to :store
  validates_presence_of :name
end

If I create two nested products on a parent store, both invalid, I will only get one validation error, observe:

s = Store.new
s.products.build(:name => nil)
s.products.build(:name => nil)
s.valid?
s.errors.full_messages

=> ["Products name can't be blank"]

I'm not sure if this is a bug, or just something that was never considered an issue. I assume the attribute (:name in this case) is overwriting the previous one, resulting in one error.

Reported by Matt Swasey · December 30th, 2009 @ 04:40 PM

State: resolved
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Eloy Duran
    Eloy Duran
    • Milestone set to 2.3.6
    • Assigned user set to Eloy Duran

    December 30th, 2009 @ 07:46 PM

  2. Eloy Duran
    Eloy Duran
    • State changed from new to hold
    • Assigned user changed from Eloy Duran to José Valim

    This is intended behavior, as can be see on this line (unless errors.on(attribute)): http://github.com/rails/rails/blob/2-3-stable/activerecord/lib/acti...

    I'm re-assigning this to José as he knows more about the current state of handling errors and I'll have a chat about it with him.

    January 4th, 2010 @ 11:37 AM

  3. José Valim
    José Valim
    • State changed from hold to open
    • Milestone cleared.

    It will get one validation error in the parent, but both children will be invalid. This is the correct behavior since you don't want error messages for to show several times the same error message:

    • Product name can't be blank
    • Product name can't be blank

    On the other hand, if the same attribute has two different errors, just the first will be shown. So we have a bug, but it's reproduced in a scenario slightly different:

    class Store < ActiveRecord::Base
      has_many :products
      accepts_nested_attributes_for :products
    end
    
    class Product < ActiveRecord::Base
      belongs_to :store
      validates_presence_of :price
      validates_numericality_of :price
    end
    
    s = Store.new
    s.products.build(:price => nil)
    s.products.build(:price => nil)
    s.valid?
    
    # It shows no information about numericality
    s.errors.full_messages => ["Products price can't be blank"]
    

    This can be fixed in master (3.0), but not easily in 2.3.6, so I'm reassigning the milestone (thanks Eloy!)

    January 4th, 2010 @ 12:50 PM

  4. José Valim
    José Valim
    • Title changed from accepts_nested_attributes_for overwriting nested validations to Different nested validations should not be ignored

    January 14th, 2010 @ 12:06 AM

  5. Repository
  6. José Valim
    José Valim
    • State changed from open to resolved

    January 14th, 2010 @ 01:07 AM

  7. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Low

    October 15th, 2010 @ 11:01 PM