Skip to content

Commit 366909d

Browse files
committed
Fix active record insert values of type cast and serialize
1 parent d21a715 commit 366909d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

activerecord/lib/active_record/insert_all.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def values_list
237237

238238
values_list = insert_all.map_key_with_value do |key, value|
239239
next value if Arel::Nodes::SqlLiteral === value
240-
connection.with_yaml_fallback(types[key].serialize(value))
240+
types[key].serialize(types[key].cast(value))
241241
end
242242

243243
connection.visitor.compile(Arel::Nodes::ValuesList.new(values_list))

activerecord/test/cases/insert_all_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ def test_insert!
4646
end
4747
end
4848

49+
def test_insert_with_type_casting_and_serialize_is_consistent
50+
skip unless supports_insert_returning?
51+
52+
book_name = ["Array"]
53+
created_book_id = Book.create!(name: book_name).id
54+
inserted_book_id = Book.insert!({ name: book_name }, returning: :id).first["id"]
55+
raw_created_book_name = Book.connection.select_value(Book.select(:name).where(id: created_book_id))
56+
raw_inserted_book_name = Book.connection.select_value(Book.select(:name).where(id: inserted_book_id))
57+
assert_equal raw_created_book_name, raw_inserted_book_name
58+
end
59+
4960
def test_insert_all
5061
assert_difference "Book.count", +10 do
5162
Book.insert_all! [

0 commit comments

Comments
 (0)