Skip to content

Commit 9ab864f

Browse files
authored
Merge pull request rails#48139 from OuYangJinTing/fix/activerecord_insert_all
Fix active record insert values of type cast and serialize
2 parents e93fb95 + 366909d commit 9ab864f

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
@@ -240,7 +240,7 @@ def values_list
240240

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

246246
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
@@ -47,6 +47,17 @@ def test_insert!
4747
end
4848
end
4949

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

0 commit comments

Comments
 (0)