Skip to content

Commit 20bc41a

Browse files
Elaborate doc for attribute read and write methods [ci-skip]
This commit elaborates the documentation for the Active Record `#[]`, `#[]=`, and `#read_attribute` methods, including adding pointers to `ActiveModel::Type` for information about specific type casting behavior. In turn, this commit removes the note about casting empty strings from the `#write_attribute` method.
1 parent 8d9f525 commit 20bc41a

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

activerecord/lib/active_record/attribute_methods.rb

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -310,36 +310,40 @@ def attribute_present?(attr_name)
310310
!value.nil? && !(value.respond_to?(:empty?) && value.empty?)
311311
end
312312

313-
# Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example,
314-
# "2004-12-12" in a date column is cast to a date object, like <tt>Date.new(2004, 12, 12)</tt>). It raises
315-
# ActiveModel::MissingAttributeError if the identified attribute is missing.
316-
#
317-
# Note: +:id+ is always present.
313+
# Returns the value of the attribute identified by +attr_name+ after it has
314+
# been type cast. (For information about specific type casting behavior, see
315+
# the types under ActiveModel::Type.)
318316
#
319317
# class Person < ActiveRecord::Base
320318
# belongs_to :organization
321319
# end
322320
#
323-
# person = Person.new(name: 'Francesco', age: '22')
324-
# person[:name] # => "Francesco"
325-
# person[:age] # => 22
321+
# person = Person.new(name: "Francesco", date_of_birth: "2004-12-12")
322+
# person[:name] # => "Francesco"
323+
# person[:date_of_birth] # => Date.new(2004, 12, 12)
324+
# person[:organization_id] # => nil
325+
#
326+
# Raises ActiveModel::MissingAttributeError if the attribute is missing.
327+
# Note, however, that the +id+ attribute will never be considered missing.
326328
#
327-
# person = Person.select('id').first
328-
# person[:name] # => ActiveModel::MissingAttributeError: missing attribute: name
329+
# person = Person.select(:name).first
330+
# person[:name] # => "Francesco"
331+
# person[:date_of_birth] # => ActiveModel::MissingAttributeError: missing attribute: date_of_birth
329332
# person[:organization_id] # => ActiveModel::MissingAttributeError: missing attribute: organization_id
333+
# person[:id] # => nil
330334
def [](attr_name)
331335
read_attribute(attr_name) { |n| missing_attribute(n, caller) }
332336
end
333337

334-
# Updates the attribute identified by <tt>attr_name</tt> with the specified +value+.
338+
# Updates the attribute identified by +attr_name+ using the specified
339+
# +value+. The attribute value will be type cast upon being read.
335340
#
336341
# class Person < ActiveRecord::Base
337342
# end
338343
#
339344
# person = Person.new
340-
# person[:age] = '22'
341-
# person[:age] # => 22
342-
# person[:age].class # => Integer
345+
# person[:date_of_birth] = "2004-12-12"
346+
# person[:date_of_birth] # => Date.new(2004, 12, 12)
343347
def []=(attr_name, value)
344348
write_attribute(attr_name, value)
345349
end

activerecord/lib/active_record/attribute_methods/read.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ def define_method_attribute(name, owner:)
2121
end
2222
end
2323

24-
# Returns the value of the attribute identified by <tt>attr_name</tt> after
25-
# it has been typecast (for example, "2004-12-12" in a date column is cast
26-
# to a date object, like <tt>Date.new(2004, 12, 12)</tt>).
24+
# Returns the value of the attribute identified by +attr_name+ after it
25+
# has been type cast. For example, a date attribute will cast "2004-12-12"
26+
# to <tt>Date.new(2004, 12, 12)</tt>. (For information about specific type
27+
# casting behavior, see the types under ActiveModel::Type.)
2728
def read_attribute(attr_name, &block)
2829
name = attr_name.to_s
2930
name = self.class.attribute_aliases[name] || name

activerecord/lib/active_record/attribute_methods/write.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ def define_method_attribute=(name, owner:)
2525
end
2626
end
2727

28-
# Updates the attribute identified by <tt>attr_name</tt> with the
29-
# specified +value+. Empty strings for Integer and Float columns are
30-
# turned into +nil+.
28+
# Updates the attribute identified by +attr_name+ using the specified
29+
# +value+. The attribute value will be type cast upon being read.
3130
def write_attribute(attr_name, value)
3231
name = attr_name.to_s
3332
name = self.class.attribute_aliases[name] || name

0 commit comments

Comments
 (0)