Skip to content

Commit c3dcae0

Browse files
authored
Merge pull request rails#54743 from Shopify/gm/fix-ar-result-column-types-regression
Fix regression in ActiveRecord::Result#column_types
2 parents 3626b8f + d3bb432 commit c3dcae0

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

activerecord/lib/active_record/result.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ def column_types
163163
@types_hash ||= begin
164164
types = {}
165165
@columns.each_with_index do |name, index|
166-
types[name] = types[index] = @column_types[index]
166+
type = @column_types[index] || Type.default_value
167+
types[name] = types[index] = type
167168
end
168169
types.freeze
169170
end

activerecord/test/cases/result_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,21 @@ def result
147147
assert_equal a.rows, b.rows
148148
assert_equal a.column_indexes, b.column_indexes
149149
end
150+
151+
test "column_types handles nil types in the column_types array" do
152+
values = [["1.1", "2.2"], ["3.3", "4.4"]]
153+
columns = ["col1", "col2"]
154+
types = [Type::Integer.new, nil] # Deliberately nil type for col2
155+
result = Result.new(columns, values, types)
156+
157+
assert_not_nil result.column_types["col1"]
158+
assert_not_nil result.column_types["col2"]
159+
160+
assert_instance_of ActiveRecord::Type::Value, result.column_types["col2"]
161+
162+
assert_nothing_raised do
163+
result.column_types["col2"].deserialize("test value")
164+
end
165+
end
150166
end
151167
end

0 commit comments

Comments
 (0)