instance method accessed_fields

Ruby on Rails edge

Since v5.0.7.2

Available in: v5.0.7.2 v5.1.7 v5.2.8.1 v6.0.6.1 v6.1.7.10 v7.0.10 v7.1.6 v7.2.4 v8.0.5.1 v8.1.4 edge

Signature

accessed_fields()

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 “‘

Source
# File activerecord/lib/active_record/attribute_methods.rb, line 525
    def accessed_fields
      @attributes.accessed
    end

Defined in activerecord/lib/active_record/attribute_methods.rb line 525 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::AttributeMethods

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close