Skip to content

Commit

Permalink
Return nil from read_attribute(:foo) if 'foo' is not present in the @…
Browse files Browse the repository at this point in the history
…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 93641ed (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.
  • Loading branch information
jonleighton committed Apr 15, 2011
1 parent e01dfb2 commit 65469a6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/attribute_methods/read.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def _#{method_name}
# "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)). # "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
def read_attribute(attr_name) def read_attribute(attr_name)
if respond_to? "_#{attr_name}" if respond_to? "_#{attr_name}"
send "_#{attr_name}" send "_#{attr_name}" if @attributes.has_key?(attr_name.to_s)
else else
_read_attribute attr_name _read_attribute attr_name
end end
Expand Down
3 changes: 2 additions & 1 deletion activerecord/test/cases/finder_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -247,9 +247,10 @@ def test_unexisting_record_exception_handling
def test_find_only_some_columns def test_find_only_some_columns
topic = Topic.find(1, :select => "author_name") topic = Topic.find(1, :select => "author_name")
assert_raise(ActiveModel::MissingAttributeError) {topic.title} assert_raise(ActiveModel::MissingAttributeError) {topic.title}
assert_nil topic.read_attribute("title")
assert_equal "David", topic.author_name assert_equal "David", topic.author_name
assert !topic.attribute_present?("title") assert !topic.attribute_present?("title")
#assert !topic.respond_to?("title") assert !topic.attribute_present?(:title)
assert topic.attribute_present?("author_name") assert topic.attribute_present?("author_name")
assert_respond_to topic, "author_name" assert_respond_to topic, "author_name"
end end
Expand Down

0 comments on commit 65469a6

Please sign in to comment.