diff options
author | José Valim <jose.valim@gmail.com> | 2012-03-17 07:13:58 -0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2012-03-17 07:13:58 -0700 |
commit | 4711298767b3d67a15f7dadeb2d8b40bb6356ad5 (patch) | |
tree | 055772ce940da94fb48bf44f72dda809d86b6e76 /activerecord/lib/active_record/validations/uniqueness.rb | |
parent | 21e530008929bc513574719b286084b025b9b036 (diff) | |
parent | 4b6daef3fc0f1edc5bfdcfb4e0a9572f1f4f56ec (diff) | |
download | rails-4711298767b3d67a15f7dadeb2d8b40bb6356ad5.tar.gz rails-4711298767b3d67a15f7dadeb2d8b40bb6356ad5.tar.bz2 rails-4711298767b3d67a15f7dadeb2d8b40bb6356ad5.zip |
Merge pull request #5321 from pfeiffer/uniqueness_validator_conditions
Add :conditions option to uniqueness validator
Diffstat (limited to 'activerecord/lib/active_record/validations/uniqueness.rb')
-rw-r--r-- | activerecord/lib/active_record/validations/uniqueness.rb | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index 9556878f63..db618f617f 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -35,8 +35,14 @@ module ActiveRecord relation = relation.and(table[scope_item].eq(scope_value)) end - if finder_class.unscoped.where(relation).exists? - record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value)) + 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 @@ -102,6 +108,14 @@ module ActiveRecord # validates_uniqueness_of :teacher_id, :scope => [:semester_id, :class_id] # end # + # It is also possible to limit the uniqueness constraint to a set of records matching certain conditions. + # In this example archived articles are not being taken into consideration when validating uniqueness + # of the title attribute: + # + # class Article < ActiveRecord::Base + # 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 # with the given value for the specified attribute (that maps to a column). When the record is updated, # the same check is made but disregarding the record itself. @@ -109,6 +123,8 @@ module ActiveRecord # Configuration options: # * <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 => 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+). |