diff --git a/activemodel/lib/active_model/validations/length.rb b/activemodel/lib/active_model/validations/length.rb index 3e6f6e62e6e08ff47e2fb7b4fab0a01f2c344232..72735cfb89f1ae770442068303e150bf0a09258c 100644 --- a/activemodel/lib/active_model/validations/length.rb +++ b/activemodel/lib/active_model/validations/length.rb @@ -42,9 +42,9 @@ def validate_each(record, attribute, value) next unless check_value = options[key] value ||= [] if key == :maximum - - next if value.kind_of?(Fixnum) && value.to_s.size.send(validity_check, check_value) - next if value && value.size.send(validity_check, check_value) + + value_length = value.respond_to?(:length) ? value.length : value.to_s.length + next if value_length.send(validity_check, check_value) errors_options = options.except(*RESERVED_OPTIONS) errors_options[:count] = check_value diff --git a/activemodel/test/cases/validations/length_validation_test.rb b/activemodel/test/cases/validations/length_validation_test.rb index 5e4f4f5cae5ea84bfd32d2f3b22f2e906c0a1b2d..f02514639b00585fb9f1b957a29e6836d6d75bfb 100644 --- a/activemodel/test/cases/validations/length_validation_test.rb +++ b/activemodel/test/cases/validations/length_validation_test.rb @@ -341,14 +341,14 @@ def test_validates_length_of_with_block assert t.errors[:content].any? assert_equal ["Your essay must be at least 5 words."], t.errors[:content] end - + def test_validates_length_of_for_fixnum Topic.validates_length_of(:approved, :is => 4) - + t = Topic.new("title" => "uhohuhoh", "content" => "whatever", :approved => 1) assert t.invalid? assert t.errors[:approved].any? - + t = Topic.new("title" => "uhohuhoh", "content" => "whatever", :approved => 1234) assert t.valid? end