aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorAndrew White <pixeltrix@users.noreply.github.com>2019-03-01 08:33:01 +0000
committerGitHub <noreply@github.com>2019-03-01 08:33:01 +0000
commit0c4bf982e80414a5437f18f988b63885359326e7 (patch)
tree95188dab2ebdd238711f6abbb5403f5d7ef83e79 /activerecord
parent8189abc1f23b78e4cad3857848a1333fd77eca37 (diff)
parentb217e6e7d318da6d9bfa52a6f68ca2b96bb9bd82 (diff)
downloadrails-0c4bf982e80414a5437f18f988b63885359326e7.tar.gz
rails-0c4bf982e80414a5437f18f988b63885359326e7.tar.bz2
rails-0c4bf982e80414a5437f18f988b63885359326e7.zip
Merge pull request #33611 from willianveiga/feature/reselect-method
Add reselect method
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md6
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb21
-rw-r--r--activerecord/test/cases/relation/select_test.rb5
3 files changed, 32 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index ce1f1102d5..0cfaf39281 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -382,6 +382,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 f69b85af66..f88493df8a 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -240,6 +240,27 @@ 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(*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:
#
# 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