diff options
author | Joe Rafaniello <jrafanie@redhat.com> | 2016-02-25 09:52:36 -0500 |
---|---|---|
committer | Joe Rafaniello <jrafanie@redhat.com> | 2016-02-25 10:13:12 -0500 |
commit | 0379da6abced76835368e7db5fe853571b055391 (patch) | |
tree | e0b055e320e2d10157265542235480871e7edc61 | |
parent | 2fda4e0874a97a76107ab9e88305169f2c625933 (diff) | |
download | rails-0379da6abced76835368e7db5fe853571b055391.tar.gz rails-0379da6abced76835368e7db5fe853571b055391.tar.bz2 rails-0379da6abced76835368e7db5fe853571b055391.zip |
Fix uniqueness validation with an after_create hook.
record.id_was is nil in after_create/after_save, so we should use
id in these cases.
While this logic feels incomplete, the existing update_record uses the same
logic:
https://github.com/rails/rails/blob/2fda4e0874a97a76107ab9e88305169f2c625933/activerecord/lib/active_record/relation.rb#L83
This logic was originally added for a similar problem:
updates not working with after_create hook.
See: 482f8c15b1d699c95bfbc3d836f674a09c0d9031
Followup to #23581
Fixes #23844
-rw-r--r-- | activerecord/lib/active_record/validations/uniqueness.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/validations/uniqueness_validation_test.rb | 18 |
2 files changed, 19 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index 13053beb78..4a80cda0b8 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -18,7 +18,7 @@ module ActiveRecord relation = build_relation(finder_class, table, attribute, value) if record.persisted? if finder_class.primary_key - relation = relation.where.not(finder_class.primary_key => record.id_was) + relation = relation.where.not(finder_class.primary_key => record.id_was || record.id) else raise UnknownPrimaryKey.new(finder_class, "Can not validate uniqueness for persisted record without primary key.") end diff --git a/activerecord/test/cases/validations/uniqueness_validation_test.rb b/activerecord/test/cases/validations/uniqueness_validation_test.rb index 8abb6c9844..4c14d93c66 100644 --- a/activerecord/test/cases/validations/uniqueness_validation_test.rb +++ b/activerecord/test/cases/validations/uniqueness_validation_test.rb @@ -53,6 +53,14 @@ class CoolTopic < Topic validates_uniqueness_of :id end +class TopicWithAfterCreate < Topic + after_create :set_author + + def set_author + update_attributes!(:author_name => "#{title} #{id}") + end +end + class UniquenessValidationTest < ActiveRecord::TestCase INT_MAX_VALUE = 2147483647 @@ -469,6 +477,16 @@ class UniquenessValidationTest < ActiveRecord::TestCase assert t.save, "Should still save t as unique" end + def test_validate_uniqueness_with_after_create_performing_save + TopicWithAfterCreate.validates_uniqueness_of(:title) + topic = TopicWithAfterCreate.create!(:title => "Title1") + assert topic.author_name.start_with?("Title1") + + topic2 = TopicWithAfterCreate.new(:title => "Title1") + refute topic2.valid? + assert_equal(["has already been taken"], topic2.errors[:title]) + end + def test_validate_uniqueness_uuid skip unless current_adapter?(:PostgreSQLAdapter) item = UuidItem.create!(uuid: SecureRandom.uuid, title: 'item1') |