In our last two blogs, part-1 and part-2, we covered many ActiveRecord methods of Rails 6. Now, let’s look at some more interesting ActiveRecord methods in this article.

ActiveRecord::Relation#extract_associated – Shorthand for Extracting Associated Record

If we want to fetch the associated records from a scoped relation, we use #preload and #collect methods on ActiveRecord::Relation before Rails 6.

Rails 6 defines #extract_associated method for extracting associated records from a relation. Let’s look at the below code snippet for a more clear view.

Before RAILS 6 (In Rails 5.2):

Output 1

#extract_associated method narrates the objective of above(#preload and #collect).


output 2

ActiveRecord::Relation#reselect – Shorthand for


Rails 6 has added #reselect method to ActiveRecord::Relation which functions similar to the methods #rewhere and #reorder. These methods are used to change the previously given conditions to new conditions.

Same way #reselect method allows us to change the previously selected attributes to new attributes.

Before Rails 6, we can perform similar operation with method chaining of 2 methods(unscope(:select).select(fields)) as below.

output 3

Now in Rails 6, it is more readable by using #reselect method.

output 4

Support for Endless Ranges in where

Ruby 2.6 has added endless ranges (1..) which is equivalent to explicitly specifying Float::INFINITY at the end of the range. Support for endless ranges added in Rails 6.
Let’s look at its different behaviors in different Ruby and Rails versions when using endless ranges with where method.

Before Ruby 2.6 and RAILS 6:

output 5

As endless ranges are not defined in the previous version of ruby, it is throwing a syntax error.

Ruby 2.6 and RAILS 5.2:

output 6

Endless ranges are defined in Ruby 2.6, so it is not throwing error but returning a wrong result as it is not supported by Rails 5.2 (in previous versions of Rails).

Ruby 2.6 and RAILS 6

output 7

In this Ruby and Rails versions, we have a correct result for using endless ranges with where method.


We have the only end of the range (1..) is available. The reverse of the same i.e., (..1) throws syntax error so there’s no need to add support for implicit –Float::INFINITY.

Allow Strong Params in ActiveRecord::Base#exists?

This commit in Rails 6 allows ActionController::Params as argument to ActiveRecord::Base#exists?. This works already for #create and #where.

Let’s look at the example


output 8

Before Rails 6:


I hope you have enjoyed this series of Rails 6 ActiveRecord methods.
Have a happy coding!!

Click here for more details…

Consulting is free – let us help you grow!