method

accessed_fields

Importance_2
v5.1.7 - Show latest stable - 0 notes - Class: ActiveRecord::AttributeMethods
accessed_fields() public

Returns the name of all database fields which have been read from this model. This can be useful in development mode to determine which fields need to be selected. For performance critical pages, selecting only the required fields can be an easy performance win (assuming you aren’t using all of the fields on the model).

For example:

class PostsController < ActionController::Base
  after_action :print_accessed_fields, only: :index

  def index
    @posts = Post.all
  end

  private

  def print_accessed_fields
    p @posts.first.accessed_fields
  end
end

Which allows you to quickly change your code to:

class PostsController < ActionController::Base
  def index
    @posts = Post.select(:id, :title, :author_id, :updated_at)
  end
end
Show source
Register or log in to add new notes.