Skip to content
This repository
Browse code

cache column defaults for AR object instantiation

  • Loading branch information...
commit 45b7209f1f8b80f33ce104185f22c61503008dd3 1 parent 06d4247
Aaron Patterson authored June 27, 2011
6  activerecord/lib/active_record/base.rb
@@ -709,6 +709,12 @@ def columns_hash
709 709
         connection_pool.columns_hash[table_name]
710 710
       end
711 711
 
  712
+      # Returns a hash where the keys are column names and the values are
  713
+      # default values when instantiating the AR object for this table.
  714
+      def column_defaults
  715
+        connection_pool.column_defaults[table_name]
  716
+      end
  717
+
712 718
       # Returns an array of column names as strings.
713 719
       def column_names
714 720
         @column_names ||= columns.map { |column| column.name }
9  activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -60,6 +60,7 @@ class ConnectionPool
60 60
       attr_accessor :automatic_reconnect
61 61
       attr_reader :spec, :connections
62 62
       attr_reader :columns, :columns_hash, :primary_keys, :tables
  63
+      attr_reader :column_defaults
63 64
 
64 65
       # Creates a new ConnectionPool object. +spec+ is a ConnectionSpecification
65 66
       # object which describes database connection information (e.g. adapter,
@@ -106,6 +107,12 @@ def initialize(spec)
106 107
           }]
107 108
         end
108 109
 
  110
+        @column_defaults = Hash.new do |h, table_name|
  111
+          h[table_name] = Hash[columns[table_name].map { |col|
  112
+            [col.name, col.default]
  113
+          }]
  114
+        end
  115
+
109 116
         @primary_keys = Hash.new do |h, table_name|
110 117
           h[table_name] = with_connection do |conn|
111 118
             table_exists?(table_name) ? conn.primary_key(table_name) : 'id'
@@ -133,6 +140,7 @@ def table_exists?(name)
133 140
       def clear_cache!
134 141
         @columns.clear
135 142
         @columns_hash.clear
  143
+        @column_defaults.clear
136 144
         @tables.clear
137 145
       end
138 146
 
@@ -140,6 +148,7 @@ def clear_cache!
140 148
       def clear_table_cache!(table_name)
141 149
         @columns.delete table_name
142 150
         @columns_hash.delete table_name
  151
+        @column_defaults.delete table_name
143 152
         @primary_keys.delete table_name
144 153
       end
145 154
 
4  activerecord/lib/active_record/persistence.rb
@@ -319,9 +319,7 @@ def create
319 319
     # that a new instance, or one populated from a passed-in Hash, still has all the attributes
320 320
     # that instances loaded from the database would.
321 321
     def attributes_from_column_definition
322  
-      Hash[self.class.columns.map do |column|
323  
-        [column.name, column.default]
324  
-      end]
  322
+      self.class.column_defaults.dup
325 323
     end
326 324
   end
327 325
 end

0 notes on commit 45b7209

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