Skip to content

Commit 96a13fc

Browse files
committed
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.
1 parent e6b4184 commit 96a13fc

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

activerecord/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## Rails 3.2.9 (unreleased)
22

3+
* Fix bug when Column is trying to type cast boolean values to integer.
4+
Fixes #8067.
5+
6+
*Rafael Mendonça França*
7+
38
* Fixed support for DATABASE_URL environment variable for rake db tasks. *Grace Liu*
49

510
* Fix bug where `update_columns` and `update_column` would not let you update the primary key column.

activerecord/lib/active_record/connection_adapters/column.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def type_cast(value)
7575

7676
case type
7777
when :string, :text then value
78-
when :integer then value.to_i
78+
when :integer then klass.value_to_integer(value)
7979
when :float then value.to_f
8080
when :decimal then klass.value_to_decimal(value)
8181
when :datetime, :timestamp then klass.string_to_time(value)
@@ -92,7 +92,7 @@ def type_cast_code(var_name)
9292

9393
case type
9494
when :string, :text then var_name
95-
when :integer then "(#{var_name}.to_i)"
95+
when :integer then "#{klass}.value_to_integer(#{var_name})"
9696
when :float then "#{var_name}.to_f"
9797
when :decimal then "#{klass}.value_to_decimal(#{var_name})"
9898
when :datetime, :timestamp then "#{klass}.string_to_time(#{var_name})"
@@ -168,6 +168,17 @@ def value_to_boolean(value)
168168
end
169169
end
170170

171+
# Used to convert values to integer.
172+
# handle the case when an integer column is used to store bollean values
173+
def value_to_integer(value)
174+
case value
175+
when TrueClass, FalseClass
176+
value ? 1 : 0
177+
else
178+
value.to_i
179+
end
180+
end
181+
171182
# convert something to a BigDecimal
172183
def value_to_decimal(value)
173184
# Using .class is faster than .is_a? and

activerecord/test/cases/column_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def test_type_cast_integer
3333
assert_equal 0, column.type_cast('bad1')
3434
assert_equal 0, column.type_cast('bad')
3535
assert_equal 1, column.type_cast(1.7)
36+
assert_equal 0, column.type_cast(false)
37+
assert_equal 1, column.type_cast(true)
3638
assert_nil column.type_cast(nil)
3739
end
3840

@@ -41,11 +43,9 @@ def test_type_cast_non_integer_to_integer
4143
assert_raises(NoMethodError) do
4244
column.type_cast([])
4345
end
46+
4447
assert_raises(NoMethodError) do
45-
column.type_cast(true)
46-
end
47-
assert_raises(NoMethodError) do
48-
column.type_cast(false)
48+
column.type_cast(Object.new)
4949
end
5050
end
5151
end

0 commit comments

Comments
 (0)