This blog is part of our Rails 5 series.
Rails makes it very easy to select all the fields of a table.
Above code is selecting all the
columns of the table
This might be ok in most cases.
However in some cases we might
want to select only certain columns
for performance reason. The difficult
task is finding what all columns are actually
used in a request.
To help in this task, Rails 5 has added accessed_fields method which lists attributes that were actually used in the operation.
This is helpful in development mode in determining what all fields are really being used by the application.
Now, in order to find all the
fields that were actually used,
after_action to the controller.
Let’s take a look at the log file.
As we can see, it returns
as attributes which were actually used.
users table has 20 columns then we do not need to
load values all those other columns. We are using
only two columns. So let’s change code to reflect that.