diff options
author | Andrew White <pixeltrix@users.noreply.github.com> | 2019-03-01 08:33:01 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-01 08:33:01 +0000 |
commit | 0c4bf982e80414a5437f18f988b63885359326e7 (patch) | |
tree | 95188dab2ebdd238711f6abbb5403f5d7ef83e79 | |
parent | 8189abc1f23b78e4cad3857848a1333fd77eca37 (diff) | |
parent | b217e6e7d318da6d9bfa52a6f68ca2b96bb9bd82 (diff) | |
download | rails-0c4bf982e80414a5437f18f988b63885359326e7.tar.gz rails-0c4bf982e80414a5437f18f988b63885359326e7.tar.bz2 rails-0c4bf982e80414a5437f18f988b63885359326e7.zip |
Merge pull request #33611 from willianveiga/feature/reselect-method
Add reselect method
-rw-r--r-- | activerecord/CHANGELOG.md | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 21 | ||||
-rw-r--r-- | activerecord/test/cases/relation/select_test.rb | 5 | ||||
-rw-r--r-- | guides/source/active_record_querying.md | 26 |
4 files changed, 58 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 diff --git a/guides/source/active_record_querying.md b/guides/source/active_record_querying.md index cb738f0657..270696d38d 100644 --- a/guides/source/active_record_querying.md +++ b/guides/source/active_record_querying.md @@ -807,6 +807,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: |