diff options
author | Arthur Klepchukov <arthur.klepchukov@gmail.com> | 2009-03-07 13:52:17 +0000 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-03-07 13:52:17 +0000 |
commit | ccb0a92fa2ba0bf7be50033090c3a861e6d907be (patch) | |
tree | 6657a66b2c19c3437837f6f2a38672d1b6827953 /activerecord | |
parent | f36513653fceb01691288dfdebfdc56d17dc85c9 (diff) | |
download | rails-ccb0a92fa2ba0bf7be50033090c3a861e6d907be.tar.gz rails-ccb0a92fa2ba0bf7be50033090c3a861e6d907be.tar.bz2 rails-ccb0a92fa2ba0bf7be50033090c3a861e6d907be.zip |
Ensure validates_uniqueness_of considers limit on the field. [#1042 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/validations.rb | 8 | ||||
-rw-r--r-- | activerecord/test/cases/validations_test.rb | 9 | ||||
-rw-r--r-- | activerecord/test/models/event.rb | 3 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 4 |
4 files changed, 20 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 8f3c80565e..4453047daf 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -720,20 +720,20 @@ module ActiveRecord # class (which has a database table to query from). finder_class = class_hierarchy.detect { |klass| !klass.abstract_class? } - is_text_column = finder_class.columns_hash[attr_name.to_s].text? + column = finder_class.columns_hash[attr_name.to_s] if value.nil? comparison_operator = "IS ?" - elsif is_text_column + elsif column.text? comparison_operator = "#{connection.case_sensitive_equality_operator} ?" - value = value.to_s + value = column.limit ? value.to_s[0, column.limit] : value.to_s else comparison_operator = "= ?" end sql_attribute = "#{record.class.quoted_table_name}.#{connection.quote_column_name(attr_name)}" - if value.nil? || (configuration[:case_sensitive] || !is_text_column) + if value.nil? || (configuration[:case_sensitive] || !column.text?) condition_sql = "#{sql_attribute} #{comparison_operator}" condition_params = [value] else diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index 8ef5dc09c5..f87168860f 100644 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -8,6 +8,7 @@ require 'models/warehouse_thing' require 'models/guid' require 'models/owner' require 'models/pet' +require 'models/event' # The following methods in Topic are used in test_conditional_validation_* class Topic @@ -530,6 +531,14 @@ class ValidationsTest < ActiveRecord::TestCase end end + def test_validate_uniqueness_with_limit + # Event.title is limited to 5 characters + e1 = Event.create(:title => "abcde") + assert e1.valid?, "Could not create an event with a unique, 5 character title" + e2 = Event.create(:title => "abcdefgh") + assert !e2.valid?, "Created an event whose title, with limit taken into account, is not unique" + end + def test_validate_straight_inheritance_uniqueness w1 = IneptWizard.create(:name => "Rincewind", :city => "Ankh-Morpork") assert w1.valid?, "Saving w1" diff --git a/activerecord/test/models/event.rb b/activerecord/test/models/event.rb new file mode 100644 index 0000000000..99fa0feeb7 --- /dev/null +++ b/activerecord/test/models/event.rb @@ -0,0 +1,3 @@ +class Event < ActiveRecord::Base + validates_uniqueness_of :title +end
\ No newline at end of file diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 74a893983f..9ebf1d82be 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -155,6 +155,10 @@ ActiveRecord::Schema.define do t.integer :course_id, :null => false end + create_table :events, :force => true do |t| + t.string :title, :limit => 5 + end + create_table :funny_jokes, :force => true do |t| t.string :name end |