Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate ActiveRecord::Result#to_hash in favor of #to_a #33912

Merged
merged 1 commit into from Sep 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,7 @@
* Deprecate `ActiveRecord::Result#to_hash` in favor of `ActiveRecord::Result#to_a`.

*Gannon McGibbon*, *Kevin Cheng*

* SQLite3 adapter supports expression indexes.

```
Expand Down
Expand Up @@ -576,7 +576,7 @@ def table_structure_with_collation(table_name, basic_structure)
column
end
else
basic_structure.to_hash
basic_structure.to_a
end
end

Expand Down
12 changes: 10 additions & 2 deletions activerecord/lib/active_record/result.rb
Expand Up @@ -21,7 +21,7 @@ module ActiveRecord
# ]
#
# # Get an array of hashes representing the result (column => value):
# result.to_hash
# result.to_a
# # => [{"id" => 1, "title" => "title_1", "body" => "body_1"},
# {"id" => 2, "title" => "title_2", "body" => "body_2"},
# ...
Expand Down Expand Up @@ -66,10 +66,18 @@ def each
end

# Returns an array of hashes representing each row record.
def to_hash
def to_a
hash_rows
end

def to_hash
ActiveSupport::Deprecation.warn(<<-MSG.squish)
`ActiveRecord::Result#to_hash` has been renamed to `to_a`.
`to_hash` is deprecated and will be removed in Rails 6.1.
MSG
to_a
end

alias :map! :map
alias :collect! :map

Expand Down
14 changes: 12 additions & 2 deletions activerecord/test/cases/result_test.rb
Expand Up @@ -21,12 +21,22 @@ def result
assert_equal 3, result.length
end

test "to_hash returns row_hashes" do
test "to_a returns row_hashes" do
assert_equal [
{ "col_1" => "row 1 col 1", "col_2" => "row 1 col 2" },
{ "col_1" => "row 2 col 1", "col_2" => "row 2 col 2" },
{ "col_1" => "row 3 col 1", "col_2" => "row 3 col 2" },
], result.to_hash
], result.to_a
end

test "to_hash (deprecated) returns row_hashes" do
assert_deprecated do
assert_equal [
{ "col_1" => "row 1 col 1", "col_2" => "row 1 col 2" },
{ "col_1" => "row 2 col 1", "col_2" => "row 2 col 2" },
{ "col_1" => "row 3 col 1", "col_2" => "row 3 col 2" },
], result.to_hash
end
end

test "first returns first row as a hash" do
Expand Down