instance method
accessed_fields
Ruby on Rails 6.1.7.10
Since v5.2.8.1Signature
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 377
def accessed_fields
@attributes.accessed
end
Defined in activerecord/lib/active_record/attribute_methods.rb line 377
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::AttributeMethods