Skip to content

Commit 07ae1e9

Browse files
committed
Unifies mysql and mysql2 casting of booleans.
1 parent 07790d5 commit 07ae1e9

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

activerecord/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
* Unify boolean type casting for `MysqlAdapter` and `Mysql2Adapter`.
2+
`type_cast` will return `1` for `true` and `0` for `false`.
3+
4+
Fixes #11119.
5+
6+
*Adam Williams*, *Yves Senn*
7+
18
* Fix bug where has_one associaton record update result in crash, when replaced with itself.
29

310
Fixes #12834.

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ def supports_index_sort_order?
206206
true
207207
end
208208

209+
def type_cast(value, column)
210+
return super unless value == true || value == false
211+
212+
value ? 1 : 0
213+
end
214+
209215
# MySQL 4 technically support transaction isolation, but it is affected by a bug
210216
# where the transaction level gets persisted for the whole session:
211217
#

activerecord/lib/active_record/connection_adapters/mysql_adapter.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,6 @@ def error_number(exception) # :nodoc:
160160

161161
# QUOTING ==================================================
162162

163-
def type_cast(value, column)
164-
return super unless value == true || value == false
165-
166-
value ? 1 : 0
167-
end
168-
169163
def quote_string(string) #:nodoc:
170164
@connection.quote(string)
171165
end

activerecord/test/cases/adapters/mysql2/boolean_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class BooleanType < ActiveRecord::Base
4646
assert_equal 1, attributes["archived"]
4747
assert_equal "1", attributes["published"]
4848

49-
assert_equal "t", @connection.type_cast(true, boolean_column)
50-
assert_equal "t", @connection.type_cast(true, string_column)
49+
assert_equal 1, @connection.type_cast(true, boolean_column)
50+
assert_equal 1, @connection.type_cast(true, string_column)
5151
end
5252

5353
test "test type casting without emulated booleans" do
@@ -60,7 +60,7 @@ class BooleanType < ActiveRecord::Base
6060
assert_equal "1", attributes["published"]
6161

6262
assert_equal 1, @connection.type_cast(true, boolean_column)
63-
assert_equal "t", @connection.type_cast(true, string_column)
63+
assert_equal 1, @connection.type_cast(true, string_column)
6464
end
6565

6666
test "with booleans stored as 1 and 0" do

0 commit comments

Comments
 (0)