diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2018-09-19 23:10:36 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-19 23:10:36 -0400 |
commit | 4586b304fc61e3a5d756d005aa2c386a1b01b848 (patch) | |
tree | 1cdf574781e879bbd6825a2b1dc4efa584d28f41 /activerecord | |
parent | d69b04de0ff33237209afea6f6cac3ab27934908 (diff) | |
parent | 16510d609c601aa7d466809f3073ec3313e08937 (diff) | |
download | rails-4586b304fc61e3a5d756d005aa2c386a1b01b848.tar.gz rails-4586b304fc61e3a5d756d005aa2c386a1b01b848.tar.bz2 rails-4586b304fc61e3a5d756d005aa2c386a1b01b848.zip |
Merge pull request #33912 from gmcgibbon/ar_result_to_hash_deprecate
Deprecate ActiveRecord::Result#to_hash in favor of #to_a
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/result.rb | 12 | ||||
-rw-r--r-- | activerecord/test/cases/result_test.rb | 14 |
4 files changed, 27 insertions, 5 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 477bc1b54a..430dd5d917 100644 --- a/activerecord/CHANGELOG.md +++ b/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. ``` diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index d32fd5ea09..baa0a29afd 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -576,7 +576,7 @@ module ActiveRecord column end else - basic_structure.to_hash + basic_structure.to_a end end diff --git a/activerecord/lib/active_record/result.rb b/activerecord/lib/active_record/result.rb index 3b2556b1c8..453331e163 100644 --- a/activerecord/lib/active_record/result.rb +++ b/activerecord/lib/active_record/result.rb @@ -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"}, # ... @@ -66,10 +66,18 @@ module ActiveRecord 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 diff --git a/activerecord/test/cases/result_test.rb b/activerecord/test/cases/result_test.rb index 68fcafb682..825aee2423 100644 --- a/activerecord/test/cases/result_test.rb +++ b/activerecord/test/cases/result_test.rb @@ -21,12 +21,22 @@ module ActiveRecord 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 |