From 607f136d52e984323cbe2938c04c660ca55a5039 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sun, 28 Apr 2013 12:03:26 +0200 Subject: fixes a test, and explains why AR::AttributeMethods checks defined?(@attributes) in some places --- activerecord/lib/active_record/attribute_methods.rb | 3 +++ activerecord/lib/active_record/core.rb | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb index 2fbab97133..609c6e8cab 100644 --- a/activerecord/lib/active_record/attribute_methods.rb +++ b/activerecord/lib/active_record/attribute_methods.rb @@ -172,6 +172,8 @@ module ActiveRecord # If the result is true then check for the select case. # For queries selecting a subset of columns, return false for unselected columns. + # We check defined?(@attributes) not to issue warnings if called on objects that + # have been allocated but not yet initialized. if defined?(@attributes) && @attributes.present? && self.class.column_names.include?(name) return has_attribute?(name) end @@ -340,6 +342,7 @@ module ActiveRecord end def attribute_method?(attr_name) # :nodoc: + # We check defined? because Syck calls respond_to? before actually calling initialize. defined?(@attributes) && @attributes.include?(attr_name) end diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index 9e45e6e474..ba053700f2 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -352,7 +352,9 @@ module ActiveRecord # Returns the contents of the record as a nicely formatted string. def inspect - inspection = if @attributes + # We check defined?(@attributes) not to issue warnings if the object is + # allocated but not initialized. + inspection = if defined?(@attributes) && @attributes self.class.column_names.collect { |name| if has_attribute?(name) "#{name}: #{attribute_for_inspect(name)}" -- cgit v1.2.3