From 00c50c2b5966fa1d719c8a58564811c672a0e8c6 Mon Sep 17 00:00:00 2001 From: Willian Gustavo Veiga Date: Mon, 13 Aug 2018 22:18:53 -0300 Subject: Add reselect method --- activerecord/lib/active_record/relation/query_methods.rb | 14 ++++++++++++++ activerecord/test/cases/relation/select_test.rb | 5 +++++ 2 files changed, 19 insertions(+) diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 52405f21a1..74103d6cdd 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -240,6 +240,20 @@ module ActiveRecord self end + # Allows you to change a previously set select statement. + # + # Post.select(:title, :body) + # # SELECT `posts.title`, `posts.body` FROM `posts` + # + # Post.select(:title, :body).reselect(:created_at) + # # SELECT `posts.created_at` FROM `posts` + # + # This is short-hand for unscope(:select).select(fields). + # Note that we're unscoping the entire select statement. + def reselect(*fields) + unscope(:select).select(*fields) + end + # Allows to specify a group attribute: # # User.group(:name) diff --git a/activerecord/test/cases/relation/select_test.rb b/activerecord/test/cases/relation/select_test.rb index dec8a6925d..32e8f473ff 100644 --- a/activerecord/test/cases/relation/select_test.rb +++ b/activerecord/test/cases/relation/select_test.rb @@ -11,5 +11,10 @@ module ActiveRecord expected = Post.select(:title).to_sql assert_equal expected, Post.select(nil).select(:title).to_sql end + + def test_reselect + expected = Post.select(:title).to_sql + assert_equal expected, Post.select(:title, :body).reselect(:title).to_sql + end end end -- cgit v1.2.3 From 43eabac605fedae72bd885c1d3861a6d1bfad3e0 Mon Sep 17 00:00:00 2001 From: Willian Gustavo Veiga Date: Tue, 2 Oct 2018 16:50:06 -0300 Subject: Add changelog entry --- activerecord/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 96fd6a62c6..6e11ecb2db 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,7 @@ +* Add `reselect` method. This is short-hand for `unscope(:select).select(fields)`. + + *Willian Gustavo Veiga* + * Added `index` option for `change_table` migration helpers. With this change you can create indexes while adding new columns into the existing tables. -- cgit v1.2.3 From 4b60e34f58c0dabe18a793038fc7775d044016f6 Mon Sep 17 00:00:00 2001 From: Willian Gustavo Veiga Date: Thu, 4 Oct 2018 21:43:26 -0300 Subject: Mention reselect new method in the "Active Record Query Interface" guide --- guides/source/active_record_querying.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/guides/source/active_record_querying.md b/guides/source/active_record_querying.md index 02055e59f0..18aa179d03 100644 --- a/guides/source/active_record_querying.md +++ b/guides/source/active_record_querying.md @@ -805,6 +805,32 @@ SELECT * FROM articles WHERE id > 10 ORDER BY id DESC LIMIT 20 ``` +### `reselect` + +The `reselect` method overrides an existing select statement. For example: + +```ruby +Post.select(:title, :body).reselect(:created_at) +``` + +The SQL that would be executed: + +```sql +SELECT `posts.created_at` FROM `posts` +``` + +In case the `reselect` clause is not used, + +```ruby +Post.select(:title, :body) +``` + +the SQL executed would be: + +```sql +SELECT `posts.title`, `posts.body` FROM `posts` +``` + ### `reorder` The `reorder` method overrides the default scope order. For example: -- cgit v1.2.3 From b217e6e7d318da6d9bfa52a6f68ca2b96bb9bd82 Mon Sep 17 00:00:00 2001 From: Willian Gustavo Veiga Date: Wed, 24 Oct 2018 20:29:13 -0300 Subject: Avoid creating an extra relation instance --- activerecord/lib/active_record/relation/query_methods.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 94c42fb630..b41cbf6f48 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -250,8 +250,15 @@ module ActiveRecord # # This is short-hand for unscope(:select).select(fields). # Note that we're unscoping the entire select statement. - def reselect(*fields) - unscope(:select).select(*fields) + def reselect(*args) + check_if_method_has_arguments!(:reselect, args) + spawn.reselect!(*args) + end + + # Same as #reselect but operates on relation in-place instead of copying. + def reselect!(*args) # :nodoc: + self.select_values = args + self end # Allows to specify a group attribute: -- cgit v1.2.3