aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorWillian Gustavo Veiga <beberveiga@gmail.com>2018-08-13 22:18:53 -0300
committerWillian Gustavo Veiga <willian.veiga@tnh.health>2018-08-13 22:19:39 -0300
commit00c50c2b5966fa1d719c8a58564811c672a0e8c6 (patch)
tree9d1fc5763be1e3690884310e1bccb054abe3d860 /activerecord
parent3000c1490565b3a21376d6444cb6fe24dfe4e383 (diff)
downloadrails-00c50c2b5966fa1d719c8a58564811c672a0e8c6.tar.gz
rails-00c50c2b5966fa1d719c8a58564811c672a0e8c6.tar.bz2
rails-00c50c2b5966fa1d719c8a58564811c672a0e8c6.zip
Add reselect method
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb14
-rw-r--r--activerecord/test/cases/relation/select_test.rb5
2 files changed, 19 insertions, 0 deletions
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 <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