aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG.md6
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb14
-rw-r--r--activerecord/test/cases/relation/select_test.rb5
-rw-r--r--guides/source/active_record_querying.md26
4 files changed, 51 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 97b7ad93d1..fd9f5c6385 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -4,6 +4,12 @@
*Federico Martinez*
+* Add `reselect` method. This is a short-hand for `unscope(:select).select(fields)`.
+
+ Fixes #27340.
+
+ *Willian Gustavo Veiga*
+
* Add basic API for connection switching to support multiple databases.
1) Adds a `connects_to` method for models to connect to multiple databases. Example:
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index eb80aab701..94c42fb630 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 <tt>unscope(:select).select(fields)</tt>.
+ # 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
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: