aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-10-29 22:47:58 -0200
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-10-29 22:47:58 -0200
commit96a13fc7de77d7e5cdadb2857b94c4e859174faa (patch)
tree7f6e7f7b79a6be74ed257925668185def2b81913
parente6b41845efe2252fe7de6882e355c31f93c65cc3 (diff)
downloadrails-96a13fc7de77d7e5cdadb2857b94c4e859174faa.tar.gz
rails-96a13fc7de77d7e5cdadb2857b94c4e859174faa.tar.bz2
rails-96a13fc7de77d7e5cdadb2857b94c4e859174faa.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.
-rw-r--r--activerecord/CHANGELOG.md5
-rw-r--r--activerecord/lib/active_record/connection_adapters/column.rb15
-rw-r--r--activerecord/test/cases/column_test.rb8
3 files changed, 22 insertions, 6 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index c762210941..9d59c91836 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 3.2.9 (unreleased)
+* Fix bug when Column is trying to type cast boolean values to integer.
+ Fixes #8067.
+
+ *Rafael Mendonça França*
+
* Fixed support for DATABASE_URL environment variable for rake db tasks. *Grace Liu*
* Fix bug where `update_columns` and `update_column` would not let you update the primary key column.
diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb
index d38e8464c5..5f8db054d4 100644
--- a/activerecord/lib/active_record/connection_adapters/column.rb
+++ b/activerecord/lib/active_record/connection_adapters/column.rb
@@ -75,7 +75,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)
@@ -92,7 +92,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})"
@@ -168,6 +168,17 @@ module ActiveRecord
end
end
+ # Used to convert values to integer.
+ # handle the case when an integer column is used to store bollean 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
diff --git a/activerecord/test/cases/column_test.rb b/activerecord/test/cases/column_test.rb
index 4fcf8a33a4..b1c1165bd9 100644
--- a/activerecord/test/cases/column_test.rb
+++ b/activerecord/test/cases/column_test.rb
@@ -33,6 +33,8 @@ module ActiveRecord
assert_equal 0, column.type_cast('bad1')
assert_equal 0, column.type_cast('bad')
assert_equal 1, column.type_cast(1.7)
+ assert_equal 0, column.type_cast(false)
+ assert_equal 1, column.type_cast(true)
assert_nil column.type_cast(nil)
end
@@ -41,11 +43,9 @@ module ActiveRecord
assert_raises(NoMethodError) do
column.type_cast([])
end
+
assert_raises(NoMethodError) do
- column.type_cast(true)
- end
- assert_raises(NoMethodError) do
- column.type_cast(false)
+ column.type_cast(Object.new)
end
end
end