diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-10-29 22:47:58 -0200 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-10-29 22:57:31 -0200 |
commit | 3525a9b5ebc87bbc9dca4b613b6d3740899190bb (patch) | |
tree | ae4b7db94cefe6add402550059cf52ed0b22a0ce /activerecord/lib/active_record/connection_adapters | |
parent | a4ac2b4d0a56ae2221b90314df1f806cb9ef9192 (diff) | |
download | rails-3525a9b5ebc87bbc9dca4b613b6d3740899190bb.tar.gz rails-3525a9b5ebc87bbc9dca4b613b6d3740899190bb.tar.bz2 rails-3525a9b5ebc87bbc9dca4b613b6d3740899190bb.zip |
Fix bug when Column is trying to type cast boolean values to integer.
This can occur if the user is using :integer columns to store boolean
values. Now we are handling the boolean values but it still raises if
the value can't type cast to integer and is not a boolean. See #7509.
Fixes #8067.
Conflicts:
activerecord/CHANGELOG.md
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/column.rb | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb index 6dcab2e0bd..76f8e899ed 100644 --- a/activerecord/lib/active_record/connection_adapters/column.rb +++ b/activerecord/lib/active_record/connection_adapters/column.rb @@ -94,7 +94,7 @@ module ActiveRecord case type when :string, :text then value - when :integer then value.to_i + when :integer then klass.value_to_integer(value) when :float then value.to_f when :decimal then klass.value_to_decimal(value) when :datetime, :timestamp then klass.string_to_time(value) @@ -115,7 +115,7 @@ module ActiveRecord case type when :string, :text then var_name - when :integer then "(#{var_name}.to_i)" + when :integer then "#{klass}.value_to_integer(#{var_name})" when :float then "#{var_name}.to_f" when :decimal then "#{klass}.value_to_decimal(#{var_name})" when :datetime, :timestamp then "#{klass}.string_to_time(#{var_name})" @@ -198,6 +198,17 @@ module ActiveRecord end end + # Used to convert values to integer. + # handle the case when an integer column is used to store boolean values + def value_to_integer(value) + case value + when TrueClass, FalseClass + value ? 1 : 0 + else + value.to_i + end + end + # convert something to a BigDecimal def value_to_decimal(value) # Using .class is faster than .is_a? and |