diff options
author | Matthew Draper <matthew@trebex.net> | 2016-11-27 23:12:31 +1030 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-27 23:12:31 +1030 |
commit | 747ddd34abf34c32b04fd4ef83ed6754f4455b55 (patch) | |
tree | 6bfd9b08508ac708bdea72aefd154c27df3c0d2d /activerecord | |
parent | c42daff32da8e75c684ba485df1d9e3868121ff3 (diff) | |
parent | 8a0fda63aa6ab9b5c30cb23a1c3f5a3399d703f8 (diff) | |
download | rails-747ddd34abf34c32b04fd4ef83ed6754f4455b55.tar.gz rails-747ddd34abf34c32b04fd4ef83ed6754f4455b55.tar.bz2 rails-747ddd34abf34c32b04fd4ef83ed6754f4455b55.zip |
Merge pull request #26865 from sergey-alekseev/uniqueness-validation-scope-with-polymorphic-association
fix the uniqueness validation scope with a polymorphic association
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/validations/uniqueness.rb | 7 | ||||
-rw-r--r-- | activerecord/test/cases/validations/uniqueness_validation_test.rb | 16 |
3 files changed, 23 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 35f7c158a6..64a0e22af2 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,7 @@ +* Fix the uniqueness validation scope with a polymorphic association. + + *Sergey Alekseev* + * Raise ActiveRecord::RecordNotFound from collection `*_ids` setters for unknown IDs with a better error message. diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index bed93bfc26..512fdadacc 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -85,11 +85,10 @@ module ActiveRecord def scope_relation(record, relation) Array(options[:scope]).each do |scope_item| - if reflection = record.class._reflect_on_association(scope_item) - scope_value = record.send(reflection.foreign_key) - scope_item = reflection.foreign_key + scope_value = if record.class._reflect_on_association(scope_item) + record.association(scope_item).reader else - scope_value = record._read_attribute(scope_item) + record._read_attribute(scope_item) end relation = relation.where(scope_item => scope_value) end diff --git a/activerecord/test/cases/validations/uniqueness_validation_test.rb b/activerecord/test/cases/validations/uniqueness_validation_test.rb index 44b4e28777..6d22638592 100644 --- a/activerecord/test/cases/validations/uniqueness_validation_test.rb +++ b/activerecord/test/cases/validations/uniqueness_validation_test.rb @@ -6,6 +6,9 @@ require "models/guid" require "models/event" require "models/dashboard" require "models/uuid_item" +require "models/author" +require "models/person" +require "models/essay" class Wizard < ActiveRecord::Base self.abstract_class = true @@ -163,6 +166,19 @@ class UniquenessValidationTest < ActiveRecord::TestCase assert !r2.valid?, "Saving r2 first time" end + def test_validate_uniqueness_with_polymorphic_object_scope + Essay.validates_uniqueness_of(:name, scope: :writer) + + a = Author.create(name: "Sergey") + p = Person.create(first_name: "Sergey") + + e1 = a.essays.create(name: "Essay") + assert e1.valid?, "Saving e1" + + e2 = p.essays.create(name: "Essay") + assert e2.valid?, "Saving e2" + end + def test_validate_uniqueness_with_composed_attribute_scope r1 = ReplyWithTitleObject.create "title" => "r1", "content" => "hello world" assert r1.valid?, "Saving r1" |