diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2008-03-31 12:57:47 +0000 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-03-31 12:57:47 +0000 |
commit | 92cd8811aa3e113053a37bccb39cf5f09d9ef84d (patch) | |
tree | 1df3ed1b15cdb5ec4461a235f961243c5c00b425 | |
parent | cc2d6a0b93251fce06bab15c52dbe0a5d5a8342c (diff) | |
download | rails-92cd8811aa3e113053a37bccb39cf5f09d9ef84d.tar.gz rails-92cd8811aa3e113053a37bccb39cf5f09d9ef84d.tar.bz2 rails-92cd8811aa3e113053a37bccb39cf5f09d9ef84d.zip |
Ensure that validates_uniqueness_of works with with_scope. Closes #9235. [nik.wakelin, cavalle]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9168 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/validations.rb | 14 | ||||
-rwxr-xr-x | activerecord/test/cases/validations_test.rb | 10 |
3 files changed, 20 insertions, 6 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index cfd3b9fb38..9ed3579318 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Ensure that validates_uniqueness_of works with with_scope. Closes #9235. [nik.wakelin, cavalle] + * Partial updates include only unsaved attributes. Off by default; set YourClass.partial_updates = true to enable. [Jeremy Kemper] * Removing unnecessary uses_tzinfo helper from tests, given that TZInfo is now bundled [Geoff Buesing] diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 5e2f710db6..72c48d1e2a 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -654,13 +654,15 @@ module ActiveRecord condition_params << record.send(:id) end - results = connection.select_all( - construct_finder_sql( - :select => "#{attr_name}", - :from => "#{finder_class.quoted_table_name}", - :conditions => [condition_sql, *condition_params] + results = finder_class.with_exclusive_scope do + connection.select_all( + construct_finder_sql( + :select => "#{attr_name}", + :from => "#{finder_class.quoted_table_name}", + :conditions => [condition_sql, *condition_params] + ) ) - ) + end unless results.length.zero? found = true diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index d0b4902bce..1bcd72c71f 100755 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -467,6 +467,16 @@ class ValidationsTest < ActiveRecord::TestCase assert i1.errors.on(:value), "Should not be empty" end + def test_validates_uniqueness_inside_with_scope + Topic.validates_uniqueness_of(:title) + + Topic.with_scope(:find => { :conditions => { :author_name => "David" } }) do + t1 = Topic.new("title" => "I'm unique!", "author_name" => "Mary") + assert t1.save + t2 = Topic.new("title" => "I'm unique!", "author_name" => "David") + assert !t2.valid? + end + end def test_validate_straight_inheritance_uniqueness w1 = IneptWizard.create(:name => "Rincewind", :city => "Ankh-Morpork") |