From 91d7673dd2ceba78794b589557c8523662517c7d Mon Sep 17 00:00:00 2001 From: Tom Ward Date: Sat, 19 Jul 2008 09:58:09 +0100 Subject: [PATCH] Ensure checked value is a string when validating case-sensitive uniqueness (#361) --- activerecord/lib/active_record/validations.rb | 2 +- activerecord/test/cases/validations_test.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 3eec130..b957ee3 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -664,7 +664,7 @@ module ActiveRecord # As MySQL/Postgres don't have case sensitive SELECT queries, we try to find duplicate # column in ruby when case sensitive option if configuration[:case_sensitive] && finder_class.columns_hash[attr_name.to_s].text? - found = results.any? { |a| a[attr_name.to_s] == value } + found = results.any? { |a| a[attr_name.to_s] == value.to_s } end if found diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index 60b00b3..e9d8fe2 100755 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -476,6 +476,15 @@ class ValidationsTest < ActiveRecord::TestCase assert !t3.errors.on(:parent_id) assert_not_equal "has already been taken", t3.errors.on(:title) end + + def test_validate_case_sensitive_uniqueness_with_attribute_passed_as_integer + Topic.validates_uniqueness_of(:title, :case_sensitve => true) + t = Topic.create!('title' => 101) + + t2 = Topic.new('title' => 101) + assert !t2.valid? + assert t2.errors.on(:title) + end def test_validate_uniqueness_with_non_standard_table_names i1 = WarehouseThing.create(:value => 1000) -- 1.5.3.7