From 3d8592657fce0c08e65d52bab00104a552b955cd Mon Sep 17 00:00:00 2001 From: Mattias Pfeiffer Date: Wed, 7 Mar 2012 15:59:28 +0100 Subject: Change syntax to accept an AR::Relation instead of old conditions hash/array. --- activerecord/lib/active_record/validations/uniqueness.rb | 12 +++++++++--- .../test/cases/validations/uniqueness_validation_test.rb | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index eb3a34c4cc..9cba2de055 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -35,7 +35,13 @@ module ActiveRecord relation = relation.and(table[scope_item].eq(scope_value)) end - if finder_class.unscoped.where(relation).where(options[:conditions]).exists? + relation = finder_class.unscoped.where(relation) + + if options[:conditions] + relation = relation.merge(options[:conditions]) + end + + if relation.exists? record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope, :conditions).merge(:value => value)) end end @@ -107,7 +113,7 @@ module ActiveRecord # of the title attribute: # # class Article < ActiveRecord::Base - # validates_uniqueness_of :title, :conditions => ['status != ?', 'archived'] + # validates_uniqueness_of :title, :conditions => where('status != ?', 'archived') # end # # When the record is created, a check is performed to make sure that no record exists in the database @@ -118,7 +124,7 @@ module ActiveRecord # * :message - Specifies a custom error message (default is: "has already been taken"). # * :scope - One or more columns by which to limit the scope of the uniqueness constraint. # * :conditions - Specify the conditions to be included as a WHERE SQL fragment to limit - # the uniqueness constraint lookup. (e.g. :conditions => {:status => 'active'}) + # the uniqueness constraint lookup. (e.g. :conditions => where('status = ?', 'active')) # * :case_sensitive - Looks for an exact match. Ignored by non-text columns (+true+ by default). # * :allow_nil - If set to true, skips this validation if the attribute is +nil+ (default is +false+). # * :allow_blank - If set to true, skips this validation if the attribute is blank (default is +false+). diff --git a/activerecord/test/cases/validations/uniqueness_validation_test.rb b/activerecord/test/cases/validations/uniqueness_validation_test.rb index 59d24f75e6..376c0000c7 100644 --- a/activerecord/test/cases/validations/uniqueness_validation_test.rb +++ b/activerecord/test/cases/validations/uniqueness_validation_test.rb @@ -327,7 +327,7 @@ class UniquenessValidationTest < ActiveRecord::TestCase end def test_validate_uniqueness_with_conditions - Topic.validates_uniqueness_of(:title, :conditions => ["approved = ?", true]) + Topic.validates_uniqueness_of(:title, :conditions => Topic.where('approved = ?', true)) t1 = Topic.create("title" => "I'm a topic", "approved" => true) t2 = Topic.create("title" => "I'm an unapproved topic", "approved" => false) -- cgit v1.2.3