diff options
author | Jon Leighton <j@jonathanleighton.com> | 2012-07-17 10:17:31 -0700 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2012-07-17 10:17:31 -0700 |
commit | d0ba994f54a4fcf72362535ec0692d4d228d8421 (patch) | |
tree | fc443a8c8c616869eab1056f1723a0c5ad9ffb25 | |
parent | 15bfaa3d13f84c689f9182c0be658ddde019e572 (diff) | |
parent | 0c3c2279e7f9b92f606356debc2efe0a93752a69 (diff) | |
download | rails-d0ba994f54a4fcf72362535ec0692d4d228d8421.tar.gz rails-d0ba994f54a4fcf72362535ec0692d4d228d8421.tar.bz2 rails-d0ba994f54a4fcf72362535ec0692d4d228d8421.zip |
Merge pull request #7072 from beerlington/fix_validations_with_scope
Use database value for uniqueness validation scope
-rw-r--r-- | activerecord/lib/active_record/validations/uniqueness.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/validations/uniqueness_validation_test.rb | 16 |
2 files changed, 17 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index 9e4b588ac2..5a24135f8e 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(options[:scope]).each do |scope_item| - scope_value = record.send(scope_item) + scope_value = record.read_attribute(scope_item) reflection = record.class.reflect_on_association(scope_item) if reflection scope_value = record.send(reflection.foreign_key) diff --git a/activerecord/test/cases/validations/uniqueness_validation_test.rb b/activerecord/test/cases/validations/uniqueness_validation_test.rb index c173ee9a15..ea5e055289 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 @@ -104,6 +112,14 @@ class UniquenessValidationTest < ActiveRecord::TestCase assert !r2.valid?, "Saving r2 first time" 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_with_object_arg Reply.validates_uniqueness_of(:topic) |