Allow ActiveRecord::QueryMethods#reselect to accept a hash

Add ability to use hash with columns and aliases inside #reselect method similar to #select which introduced in https://github.com/rails/rails/pull/45612
This commit is contained in:
sampatbadhe 2022-10-16 15:17:26 +05:30
parent d54f0c52fa
commit 90e96e5122
3 changed files with 17 additions and 0 deletions

View File

@ -1,3 +1,7 @@
* Allow ActiveRecord::QueryMethods#reselect to receive hash values, similar to ActiveRecord::QueryMethods#select
*Sampat Badhe*
* Validate options when managing columns and tables in migrations.
If an invalid option is passed to a migration method like `create_table` and `add_column`, an error will be raised

View File

@ -406,6 +406,7 @@ module ActiveRecord
# Note that we're unscoping the entire select statement.
def reselect(*args)
check_if_method_has_arguments!(__callee__, args)
args = process_select_args(args)
spawn.reselect!(*args)
end

View File

@ -80,6 +80,18 @@ module ActiveRecord
assert_equal expected, actual
end
def test_reselect_with_hash_argument
expected = Post.select(:title, posts: { title: :post_title }).to_sql
actual = Post.select(:title, :body).reselect(:title, posts: { title: :post_title }).to_sql
assert_equal expected, actual
end
def test_reselect_with_one_level_hash_argument
expected = Post.select(:title, title: :post_title).to_sql
actual = Post.select(:title, :body).reselect(:title, title: :post_title).to_sql
assert_equal expected, actual
end
def test_non_select_columns_wont_be_loaded
posts = Post.select("UPPER(title) AS title")