Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11398: fix_type_casting_so_float_columns_behave_similarly_to_integer_columns.diff

File fix_type_casting_so_float_columns_behave_similarly_to_integer_columns.diff, 1.3 kB (added by h-lame, 7 months ago)

Tiny fix to schema_definitions.rb that will make float column type casting behave similarly to integers

  • activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb

    old new  
    6060          when :string    then value 
    6161          when :text      then value 
    6262          when :integer   then value.to_i rescue value ? 1 : 0 
    63           when :float     then value.to_f 
     63          when :float     then value.to_f rescue value.to_s.to_f 
    6464          when :decimal   then self.class.value_to_decimal(value) 
    6565          when :datetime  then self.class.string_to_time(value) 
    6666          when :timestamp then self.class.string_to_time(value) 
     
    7777          when :string    then nil 
    7878          when :text      then nil 
    7979          when :integer   then "(#{var_name}.to_i rescue #{var_name} ? 1 : 0)" 
    80           when :float     then "#{var_name}.to_f
     80          when :float     then "#{var_name}.to_f rescue #{var_name}.to_s.to_f
    8181          when :decimal   then "#{self.class.name}.value_to_decimal(#{var_name})" 
    8282          when :datetime  then "#{self.class.name}.string_to_time(#{var_name})" 
    8383          when :timestamp then "#{self.class.name}.string_to_time(#{var_name})"