Skip to content
This repository
Browse code

Define to_ary on ActiveRecord::Base to return nil. Improve performanc…

…e of Array#flatten under 1.9, see comment for details.
  • Loading branch information...
commit e9fa602e1c70a5c8e1644b218d5784e7c195eb35 1 parent 9378d7e
Jon Leighton authored June 30, 2011

Showing 1 changed file with 12 additions and 0 deletions. Show diff stats Hide diff stats

  1. 12  activerecord/lib/active_record/base.rb
12  activerecord/lib/active_record/base.rb
@@ -1900,6 +1900,18 @@ def clone_attribute_value(reader_method, attribute_name)
1900 1900
 
1901 1901
     private
1902 1902
 
  1903
+      # Under Ruby 1.9, Array#flatten will call #to_ary (recursively) on each of the elements
  1904
+      # of the array, and then rescues from the possible NoMethodError. If those elements are
  1905
+      # ActiveRecord::Base's, then this triggers the various method_missing's that we have,
  1906
+      # which significantly impacts upon performance.
  1907
+      #
  1908
+      # So we can avoid the method_missing hit by explicitly defining #to_ary as nil here.
  1909
+      #
  1910
+      # See also http://tenderlovemaking.com/2011/06/28/til-its-ok-to-return-nil-from-to_ary/
  1911
+      def to_ary # :nodoc:
  1912
+        nil
  1913
+      end
  1914
+
1903 1915
       def set_serialized_attributes
1904 1916
         sattrs = self.class.serialized_attributes
1905 1917
 

0 notes on commit e9fa602

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