Skip to content

Commit 1869a5a

Browse files
authored
Merge pull request rails#53472 from p8/activerecord/improve-attributes-for-inspect-docs
Expand documentation of `.attributes_for_inspect` [ci-skip]
2 parents d20f58b + 54b02d0 commit 1869a5a

File tree

1 file changed

+29
-3
lines changed
  • activerecord/lib/active_record

1 file changed

+29
-3
lines changed

activerecord/lib/active_record/core.rb

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,19 @@ def self.configurations
103103

104104
class_attribute :shard_selector, instance_accessor: false, default: nil
105105

106-
# Specifies the attributes that will be included in the output of the #inspect method
106+
##
107+
# :singleton-method:
108+
#
109+
# Specifies the attributes that will be included in the output of the
110+
# #inspect method:
111+
#
112+
# Post.attributes_for_inspect = [:id, :title]
113+
# Post.first.inspect #=> "#<Post id: 1, title: "Hello, World!">"
114+
#
115+
# When set to `:all` inspect will list all the record's attributes:
116+
#
117+
# Post.attributes_for_inspect = :all
118+
# Post.first.inspect #=> "#<Post id: 1, title: "Hello, World!", published_at: "2023-10-23 14:28:11 +0000">"
107119
class_attribute :attributes_for_inspect, instance_accessor: false, default: :all
108120

109121
def self.application_record_class? # :nodoc:
@@ -728,12 +740,26 @@ def connection_handler
728740
self.class.connection_handler
729741
end
730742

731-
# Returns the attributes specified by <tt>.attributes_for_inspect</tt> as a nicely formatted string.
743+
# Returns the attributes of the record as a nicely formatted string.
744+
#
745+
# Post.first.inspect
746+
# #=> "#<Post id: 1, title: "Hello, World!", published_at: "2023-10-23 14:28:11 +0000">"
747+
#
748+
# The attributes can be limited by setting <tt>.attributes_for_inspect</tt>.
749+
#
750+
# Post.attributes_for_inspect = [:id, :title]
751+
# Post.first.inspect
752+
# #=> "#<Post id: 1, title: "Hello, World!">"
732753
def inspect
733754
inspect_with_attributes(attributes_for_inspect)
734755
end
735756

736-
# Returns the full contents of the record as a nicely formatted string.
757+
# Returns all attributes of the record as a nicely formatted string,
758+
# ignoring <tt>.attributes_for_inspect</tt>.
759+
#
760+
# Post.first.full_inspect
761+
# #=> "#<Post id: 1, title: "Hello, World!", published_at: "2023-10-23 14:28:11 +0000">"
762+
#
737763
def full_inspect
738764
inspect_with_attributes(all_attributes_for_inspect)
739765
end

0 commit comments

Comments
 (0)