This project is archived and is in readonly mode.
Different nested validations should not be ignored
Reported by Matt Swasey | December 30th, 2009 @ 04:40 PM | in 3.0.2
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.
Comments and changes to this ticket
-
Eloy Duran December 30th, 2009 @ 07:46 PM
- Milestone set to 2.3.6
- Assigned user set to Eloy Duran
-
Eloy Duran January 4th, 2010 @ 11:37 AM
- 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.
-
José Valim January 4th, 2010 @ 12:50 PM
- 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!)
-
José Valim January 14th, 2010 @ 12:06 AM
- Title changed from accepts_nested_attributes_for overwriting nested validations to Different nested validations should not be ignored
-
Repository January 14th, 2010 @ 01:03 AM
(from [363545aa20014c56f6da223acc4a46de5c873143]) Different nested validations should not be ignore [#3638 status:resolved] http://github.com/rails/rails/commit/363545aa20014c56f6da223acc4a46...
-
José Valim January 14th, 2010 @ 01:07 AM
- State changed from open to resolved
-
Jeremy Kemper October 15th, 2010 @ 11:01 PM
- Milestone set to 3.0.2
- Importance changed from to Low
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Referenced by
- 3638 Different nested validations should not be ignored (from [363545aa20014c56f6da223acc4a46de5c873143]) Differe...