aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorNarihiro Nakamura <authornari@gmail.com>2013-02-26 14:39:26 +0900
committerNarihiro Nakamura <authornari@gmail.com>2013-02-26 14:39:26 +0900
commit905b7df1fac8332547b9eb7fc4d5b083c6fc1031 (patch)
treea6b8de1e96448eb6959b166090e6647b12b448a1 /activerecord
parent0761bb029886bb6920a404ecf409013f83a44f58 (diff)
downloadrails-905b7df1fac8332547b9eb7fc4d5b083c6fc1031.tar.gz
rails-905b7df1fac8332547b9eb7fc4d5b083c6fc1031.tar.bz2
rails-905b7df1fac8332547b9eb7fc4d5b083c6fc1031.zip
Backported #7072 to 3-2-stable. Use database value for uniqueness validation scope.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md6
-rw-r--r--activerecord/lib/active_record/validations/uniqueness.rb2
-rw-r--r--activerecord/test/cases/validations/uniqueness_validation_test.rb16
3 files changed, 23 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index db4820b3d2..738eddb406 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,4 +1,10 @@
## unreleased ##
+* Fixes issue with overrding ActiveRecord reader methods with a
+ composed object and using that attribute as the scope of a
+ validates_uniqueness_of validation.
+ Backport of #7072
+
+ *Peter Brown*
* Sqlite now preserves custom primary keys when copying or altering tables.
Fixes #9367.
diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb
index 154aacef9f..795088530e 100644
--- a/activerecord/lib/active_record/validations/uniqueness.rb
+++ b/activerecord/lib/active_record/validations/uniqueness.rb
@@ -26,7 +26,7 @@ module ActiveRecord
relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.send(:id))) if record.persisted?
Array.wrap(options[:scope]).each do |scope_item|
- scope_value = record.send(scope_item)
+ scope_value = record.read_attribute(scope_item)
relation = relation.and(table[scope_item].eq(scope_value))
end
diff --git a/activerecord/test/cases/validations/uniqueness_validation_test.rb b/activerecord/test/cases/validations/uniqueness_validation_test.rb
index 8708117129..0f9cb10f0f 100644
--- a/activerecord/test/cases/validations/uniqueness_validation_test.rb
+++ b/activerecord/test/cases/validations/uniqueness_validation_test.rb
@@ -22,6 +22,14 @@ end
class Thaumaturgist < IneptWizard
end
+class ReplyTitle; end
+
+class ReplyWithTitleObject < Reply
+ validates_uniqueness_of :content, :scope => :title
+
+ def title; ReplyTitle.new; end
+end
+
class UniquenessValidationTest < ActiveRecord::TestCase
fixtures :topics, 'warehouse-things', :developers
@@ -80,6 +88,14 @@ class UniquenessValidationTest < ActiveRecord::TestCase
assert r3.valid?, "Saving r3"
end
+ def test_validate_uniqueness_with_composed_attribute_scope
+ r1 = ReplyWithTitleObject.create "title" => "r1", "content" => "hello world"
+ assert r1.valid?, "Saving r1"
+
+ r2 = ReplyWithTitleObject.create "title" => "r1", "content" => "hello world"
+ assert !r2.valid?, "Saving r2 first time"
+ end
+
def test_validate_uniqueness_scoped_to_defining_class
t = Topic.create("title" => "What, me worry?")