aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/validations/uniqueness.rb
diff options
context:
space:
mode:
authorRaimonds Simanovskis <raimonds.simanovskis@gmail.com>2011-04-25 22:09:48 +0300
committerRaimonds Simanovskis <raimonds.simanovskis@gmail.com>2011-04-25 22:17:18 +0300
commit070c9984a50a5d715a8d2cd3847ae4b603a10d19 (patch)
tree06b7f95a1e774383cd5f07e4cf64e081b036573a /activerecord/lib/active_record/validations/uniqueness.rb
parent12427c8dea1e817d7badc06f1f21af989c0f56ea (diff)
downloadrails-070c9984a50a5d715a8d2cd3847ae4b603a10d19.tar.gz
rails-070c9984a50a5d715a8d2cd3847ae4b603a10d19.tar.bz2
rails-070c9984a50a5d715a8d2cd3847ae4b603a10d19.zip
Do not use SQL LIKE operator for case insensitive uniqueness validation
It can result in wrong results if values contain special % or _ characters. It is safer to use SQL LOWER function and compare for equality.
Diffstat (limited to 'activerecord/lib/active_record/validations/uniqueness.rb')
-rw-r--r--activerecord/lib/active_record/validations/uniqueness.rb5
1 files changed, 3 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb
index d1225a9ed9..4db4105389 100644
--- a/activerecord/lib/active_record/validations/uniqueness.rb
+++ b/activerecord/lib/active_record/validations/uniqueness.rb
@@ -56,8 +56,9 @@ module ActiveRecord
column = klass.columns_hash[attribute.to_s]
value = column.limit ? value.to_s.mb_chars[0, column.limit] : value.to_s if column.text?
- if !options[:case_sensitive] && column.text?
- relation = table[attribute].matches(value)
+ if !options[:case_sensitive] && value && column.text?
+ # will use SQL LOWER function before comparison
+ relation = table[attribute].lower.eq(table.lower(value))
else
value = klass.connection.case_sensitive_modifier(value)
relation = table[attribute].eq(value)