Skip to content
This repository
Browse code

Cast number to string in Postgres

fixes #9170
  • Loading branch information...
commit aae89ee8607a802557b4877feb995204e0b58fed 1 parent d25a822
Łukasz Strzałkowski authored March 12, 2013
9  activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb
@@ -51,9 +51,12 @@ def quote(value, column = nil) #:nodoc:
51 51
               super
52 52
             end
53 53
           when Numeric
54  
-            return super unless column.sql_type == 'money'
55  
-            # Not truly string input, so doesn't require (or allow) escape string syntax.
56  
-            "'#{value}'"
  54
+            if column.sql_type == 'money' || [:string, :text].include?(column.type)
  55
+              # Not truly string input, so doesn't require (or allow) escape string syntax.
  56
+              "'#{value}'"
  57
+            else
  58
+              super
  59
+            end
57 60
           when String
58 61
             case column.sql_type
59 62
             when 'bytea' then "'#{escape_bytea(value)}'"
8  activerecord/test/cases/adapters/postgresql/quoting_test.rb
@@ -44,6 +44,14 @@ def test_quote_float_infinity
44 44
           c = Column.new(nil, 1, 'float')
45 45
           assert_equal "'Infinity'", @conn.quote(infinity, c)
46 46
         end
  47
+
  48
+        def test_quote_cast_numeric
  49
+          fixnum = 666
  50
+          c = Column.new(nil, nil, 'string')
  51
+          assert_equal "'666'", @conn.quote(fixnum, c)
  52
+          c = Column.new(nil, nil, 'text')
  53
+          assert_equal "'666'", @conn.quote(fixnum, c)
  54
+        end
47 55
       end
48 56
     end
49 57
   end

0 notes on commit aae89ee

Please sign in to comment.
Something went wrong with that request. Please try again.