This blog is part of our Rails 6 series.
Rails have
rewhere and
reorder methods
to change the previously set conditions attributes to new attributes which are
given as an argument to method.
Before Rails 6, if you want to change the previously set select statement
attributes to new attributes, it was done as follows.
>> Post.select(:title, :body).unscope(:select).select(:views)
SELECT "posts"."views" FROM "posts" LIMIT ? ["LIMIT", 1]]
In Rails 6, ActiveRecord::Relation#reselect method is added.
The reselect method is similar to rewhere and reorder. reselect is a
short-hand for unscope(:select).select(fields).
Here is how reselect method can be used.
>> Post.select(:title, :body).reselect(:views)
SELECT "posts"."views" FROM "posts" LIMIT ? ["LIMIT", 1]]
Check out the pull request for more details on this.

Written byAbhay Nikam
Related blogs
- 01Rails 6 adds support to persist timezones of Active JobSeptember 1, 2020 · Chetan Gawai
- 02Rails 6 adds including & excluding method on EnumerablesAugust 18, 2020 · Akhil Gautam
- 03Rails 6 fixes after_commit callback invocation bugFebruary 25, 2020 · Amit Choudhary
- 04Rails 6 adds rails db:prepare to migrate or setup a databaseDecember 10, 2019 · Akhil Gautam