Skip to content
This repository
Browse code

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 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...
commit 65469a6e5e8cd2418c99c3862dd33feed69536bd 1 parent e01dfb2
Jon Leighton jonleighton authored
2  activerecord/lib/active_record/attribute_methods/read.rb
@@ -100,7 +100,7 @@ def _#{method_name}
100 100 # "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
101 101 def read_attribute(attr_name)
102 102 if respond_to? "_#{attr_name}"
103   - send "_#{attr_name}"
  103 + send "_#{attr_name}" if @attributes.has_key?(attr_name.to_s)
104 104 else
105 105 _read_attribute attr_name
106 106 end
3  activerecord/test/cases/finder_test.rb
@@ -247,9 +247,10 @@ def test_unexisting_record_exception_handling
247 247 def test_find_only_some_columns
248 248 topic = Topic.find(1, :select => "author_name")
249 249 assert_raise(ActiveModel::MissingAttributeError) {topic.title}
  250 + assert_nil topic.read_attribute("title")
250 251 assert_equal "David", topic.author_name
251 252 assert !topic.attribute_present?("title")
252   - #assert !topic.respond_to?("title")
  253 + assert !topic.attribute_present?(:title)
253 254 assert topic.attribute_present?("author_name")
254 255 assert_respond_to topic, "author_name"
255 256 end

0 comments on commit 65469a6

Please sign in to comment.
Something went wrong with that request. Please try again.