mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Change uniq validation with conditions examples to use where with hashes
Take the opportunity to showcase where.not.
This commit is contained in:
parent
f56cdbdfb2
commit
6e5ab6fccb
2 changed files with 5 additions and 5 deletions
|
@ -4,7 +4,7 @@ module ActiveRecord
|
|||
def initialize(options)
|
||||
if options[:conditions] && !options[:conditions].respond_to?(:call)
|
||||
raise ArgumentError, "#{options[:conditions]} was passed as :conditions but is not callable. " \
|
||||
"Pass a callable instead: `conditions: -> { where('approved = ?', true) }`"
|
||||
"Pass a callable instead: `conditions: -> { where(approved: true) }`"
|
||||
end
|
||||
super({ case_sensitive: true }.merge!(options))
|
||||
end
|
||||
|
@ -120,7 +120,7 @@ module ActiveRecord
|
|||
# of the title attribute:
|
||||
#
|
||||
# class Article < ActiveRecord::Base
|
||||
# validates_uniqueness_of :title, conditions: -> { where('status != ?', 'archived') }
|
||||
# validates_uniqueness_of :title, conditions: -> { where.not(status: 'archived') }
|
||||
# end
|
||||
#
|
||||
# When the record is created, a check is performed to make sure that no
|
||||
|
@ -136,7 +136,7 @@ module ActiveRecord
|
|||
# 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>).
|
||||
# (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
|
||||
|
|
|
@ -348,7 +348,7 @@ class UniquenessValidationTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_validate_uniqueness_with_conditions
|
||||
Topic.validates_uniqueness_of :title, conditions: -> { where('approved = ?', true) }
|
||||
Topic.validates_uniqueness_of :title, conditions: -> { where(approved: true) }
|
||||
Topic.create("title" => "I'm a topic", "approved" => true)
|
||||
Topic.create("title" => "I'm an unapproved topic", "approved" => false)
|
||||
|
||||
|
@ -361,7 +361,7 @@ class UniquenessValidationTest < ActiveRecord::TestCase
|
|||
|
||||
def test_validate_uniqueness_with_non_callable_conditions_is_not_supported
|
||||
assert_raises(ArgumentError) {
|
||||
Topic.validates_uniqueness_of :title, conditions: Topic.where('approved = ?', true)
|
||||
Topic.validates_uniqueness_of :title, conditions: Topic.where(approved: true)
|
||||
}
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue