Skip to content

Commit 88fb34b

Browse files
committed
ActiveRecord::Result#each to yield regular Hash
Followup: rails#51744 Fix: rails#53081 Closes: rails#53088 While the yielded type wasn't specified and we could always return the more efficient format, there's too many code out there that depend on this behavior. Ultimately, queries done with `exec_query` are rare, as long as Active Record itself use the more efficient path to instantiate records, most of the gain is obtained.
1 parent a79c753 commit 88fb34b

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,8 +725,6 @@ def table_structure_with_collation(table_name, basic_structure)
725725
end
726726

727727
basic_structure.map do |column|
728-
column = column.to_h
729-
730728
column_name = column["name"]
731729

732730
if collation_hash.has_key? column_name

activerecord/lib/active_record/querying.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ def _load_from_sql(result_set, &block) # :nodoc:
8686

8787
message_bus.instrument("instantiation.active_record", payload) do
8888
if result_set.includes_column?(inheritance_column)
89-
result_set.map { |record| instantiate(record, column_types, &block) }
89+
result_set.indexed_rows.map { |record| instantiate(record, column_types, &block) }
9090
else
9191
# Instantiate a homogeneous set
92-
result_set.map { |record| instantiate_instance_of(self, record, column_types, &block) }
92+
result_set.indexed_rows.map { |record| instantiate_instance_of(self, record, column_types, &block) }
9393
end
9494
end
9595
end

activerecord/lib/active_record/result.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ def length
127127
# Returns an +Enumerator+ if no block is given.
128128
def each(&block)
129129
if block_given?
130-
indexed_rows.each(&block)
130+
hash_rows.each(&block)
131131
else
132-
indexed_rows.to_enum { @rows.size }
132+
hash_rows.to_enum { @rows.size }
133133
end
134134
end
135135

@@ -191,6 +191,7 @@ def cast_values(type_overrides = {}) # :nodoc:
191191
def initialize_copy(other)
192192
@rows = rows.dup
193193
@column_types = column_types.dup
194+
@hash_rows = nil
194195
end
195196

196197
def freeze # :nodoc:
@@ -212,6 +213,13 @@ def column_indexes # :nodoc:
212213
end
213214
end
214215

216+
def indexed_rows # :nodoc:
217+
@indexed_rows ||= begin
218+
columns = column_indexes
219+
@rows.map { |row| IndexedRow.new(columns, row) }.freeze
220+
end
221+
end
222+
215223
private
216224
def column_type(name, index, type_overrides)
217225
type_overrides.fetch(name) do
@@ -221,13 +229,6 @@ def column_type(name, index, type_overrides)
221229
end
222230
end
223231

224-
def indexed_rows
225-
@indexed_rows ||= begin
226-
columns = column_indexes
227-
@rows.map { |row| IndexedRow.new(columns, row) }.freeze
228-
end
229-
end
230-
231232
def hash_rows
232233
# We use transform_values to rows.
233234
# This is faster because we avoid any reallocs and avoid hashing entirely.

0 commit comments

Comments
 (0)