diff options
author | José Valim <jose.valim@gmail.com> | 2011-05-25 05:02:15 -0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-05-25 05:02:15 -0700 |
commit | b145a725f84b9f01a63370249f61c61725ab8927 (patch) | |
tree | 2dea07f6d5c009ea0c2ee062f0bc8af90948e5ce | |
parent | b1bb9d17381c860653db50589774e83cb5c95293 (diff) | |
parent | b2db8740eabbb9b9c6afb237530d33ef7fc807e9 (diff) | |
download | rails-b145a725f84b9f01a63370249f61c61725ab8927.tar.gz rails-b145a725f84b9f01a63370249f61c61725ab8927.tar.bz2 rails-b145a725f84b9f01a63370249f61c61725ab8927.zip |
Merge pull request #1297 from cesario/1294-allocated-object-cant-be-inspected
#1294 : allocated object cant be inspected [closes #1294]
-rw-r--r-- | activerecord/lib/active_record/base.rb | 16 | ||||
-rw-r--r-- | activerecord/test/cases/attribute_methods_test.rb | 8 |
2 files changed, 18 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 99930e7697..cb2c621c79 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1870,12 +1870,16 @@ MSG # Returns the contents of the record as a nicely formatted string. def inspect - attributes_as_nice_string = self.class.column_names.collect { |name| - if has_attribute?(name) - "#{name}: #{attribute_for_inspect(name)}" - end - }.compact.join(", ") - "#<#{self.class} #{attributes_as_nice_string}>" + inspection = if @attributes + self.class.column_names.collect { |name| + if has_attribute?(name) + "#{name}: #{attribute_for_inspect(name)}" + end + }.compact.join(", ") + else + "not initialized" + end + "#<#{self.class} #{inspection}>" end protected diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index 5074ae50ab..54c4d4ae90 100644 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -109,6 +109,14 @@ class AttributeMethodsTest < ActiveRecord::TestCase assert_respond_to topic, :title end + # IRB inspects the return value of "MyModel.allocate" + # by inspecting it. + def test_allocated_object_can_be_inspected + topic = Topic.allocate + assert_nothing_raised { topic.inspect } + assert topic.inspect, "#<Topic not initialized>" + end + def test_array_content topic = Topic.new topic.content = %w( one two three ) |