mirror of
https://github.com/github/rails.git
synced 2026-01-12 08:08:31 -05:00
Return nil from read_attribute(:foo) if 'foo' is not present in the @attributes hash, but the _foo method has been defined. This brings the behaviour into line with the 3-0-stable branch and the master branch before 93641ed6c8 (there were previously no assertions about this which is why the change slipped through). Note that actually calling the 'foo' method will still raise an error if the attribute is not present.
This commit is contained in:
@@ -100,7 +100,7 @@ module ActiveRecord
|
||||
# "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
|
||||
def read_attribute(attr_name)
|
||||
if respond_to? "_#{attr_name}"
|
||||
send "_#{attr_name}"
|
||||
send "_#{attr_name}" if @attributes.has_key?(attr_name.to_s)
|
||||
else
|
||||
_read_attribute attr_name
|
||||
end
|
||||
|
||||
@@ -247,9 +247,10 @@ class FinderTest < ActiveRecord::TestCase
|
||||
def test_find_only_some_columns
|
||||
topic = Topic.find(1, :select => "author_name")
|
||||
assert_raise(ActiveModel::MissingAttributeError) {topic.title}
|
||||
assert_nil topic.read_attribute("title")
|
||||
assert_equal "David", topic.author_name
|
||||
assert !topic.attribute_present?("title")
|
||||
#assert !topic.respond_to?("title")
|
||||
assert !topic.attribute_present?(:title)
|
||||
assert topic.attribute_present?("author_name")
|
||||
assert_respond_to topic, "author_name"
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user