aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Pfeiffer <mattias@pfeiffer.dk>2012-03-07 15:59:28 +0100
committerMattias Pfeiffer <mattias@pfeiffer.dk>2012-03-07 15:59:28 +0100
commit3d8592657fce0c08e65d52bab00104a552b955cd (patch)
tree0894ef760ed00696c2186c791b40e6d5c235ca4b
parent6185c4947e88f1b6b7c2adaabf62081bac6df04a (diff)
downloadrails-3d8592657fce0c08e65d52bab00104a552b955cd.tar.gz
rails-3d8592657fce0c08e65d52bab00104a552b955cd.tar.bz2
rails-3d8592657fce0c08e65d52bab00104a552b955cd.zip
Change syntax to accept an AR::Relation instead of old conditions hash/array.
-rw-r--r--activerecord/lib/active_record/validations/uniqueness.rb12
-rw-r--r--activerecord/test/cases/validations/uniqueness_validation_test.rb2
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
# * <tt>:message</tt> - Specifies a custom error message (default is: "has already been taken").
# * <tt>:scope</tt> - One or more columns by which to limit the scope of the uniqueness constraint.
# * <tt>:conditions</tt> - Specify the conditions to be included as a <tt>WHERE</tt> SQL fragment to limit
- # the uniqueness constraint lookup. (e.g. <tt>:conditions => {:status => 'active'}</tt>)
+ # the uniqueness constraint lookup. (e.g. <tt>:conditions => where('status = ?', 'active')</tt>)
# * <tt>:case_sensitive</tt> - Looks for an exact match. Ignored by non-text columns (+true+ by default).
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+).
# * <tt>:allow_blank</tt> - 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)