diff options
author | José Valim <jose.valim@plataformatec.com.br> | 2012-10-17 02:47:38 -0700 |
---|---|---|
committer | José Valim <jose.valim@plataformatec.com.br> | 2012-10-17 02:47:38 -0700 |
commit | bf876af7b33b8f6a4c91353e2751686f1d0fa934 (patch) | |
tree | 48baee2cc9088c4eed11696e9c280718cf175211 | |
parent | c432c74cd34d4230bfda6fa008f2af9dbc33e523 (diff) | |
parent | 25262bc280e8c9c0e875315958f82230b67cbf35 (diff) | |
download | rails-bf876af7b33b8f6a4c91353e2751686f1d0fa934.tar.gz rails-bf876af7b33b8f6a4c91353e2751686f1d0fa934.tar.bz2 rails-bf876af7b33b8f6a4c91353e2751686f1d0fa934.zip |
Merge pull request #7972 from scottwillson/fix-double-presence-of-errors-2
Fix bug with presence validation of associations.
-rw-r--r-- | activerecord/CHANGELOG.md | 5 | ||||
-rw-r--r-- | activerecord/lib/active_record/validations/presence.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/validations/presence_validation_test.rb | 7 |
3 files changed, 16 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 5f5c9e4915..186c7bf244 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,10 @@ ## Rails 4.0.0 (unreleased) ## +* Fix bug with presence validation of associations. Would incorrectly add duplicated errors + when the association was blank. Bug introduced in 1fab518c6a75dac5773654646eb724a59741bc13. + + *Scott Willson* + * Fix bug where sum(expression) returns string '0' for no matching records Fixes #7439 diff --git a/activerecord/lib/active_record/validations/presence.rb b/activerecord/lib/active_record/validations/presence.rb index 81a3521d24..6b14c39686 100644 --- a/activerecord/lib/active_record/validations/presence.rb +++ b/activerecord/lib/active_record/validations/presence.rb @@ -5,8 +5,10 @@ module ActiveRecord super attributes.each do |attribute| next unless record.class.reflect_on_association(attribute) - value = record.send(attribute) - if Array(value).all? { |r| r.marked_for_destruction? } + associated_records = Array(record.send(attribute)) + + # Superclass validates presence. Ensure present records aren't about to be destroyed. + if associated_records.present? && associated_records.all? { |r| r.marked_for_destruction? } record.errors.add(attribute, :blank, options) end end diff --git a/activerecord/test/cases/validations/presence_validation_test.rb b/activerecord/test/cases/validations/presence_validation_test.rb index cd9175f454..1de8934406 100644 --- a/activerecord/test/cases/validations/presence_validation_test.rb +++ b/activerecord/test/cases/validations/presence_validation_test.rb @@ -18,6 +18,13 @@ class PresenceValidationTest < ActiveRecord::TestCase assert b.valid? end + def test_validates_presence_of_has_one + Boy.validates_presence_of(:face) + b = Boy.new + assert b.invalid?, "should not be valid if has_one association missing" + assert_equal 1, b.errors[:face].size, "validates_presence_of should only add one error" + end + def test_validates_presence_of_has_one_marked_for_destruction Boy.validates_presence_of(:face) b = Boy.new |