From 0b08ff7d92124cc370e9f0795d1559204f04f9a4 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Fri, 16 Dec 2011 11:46:34 +0000 Subject: Cache columns at the model level. Allows two models to use the same table but have different primary keys. --- .../lib/active_record/attribute_methods/primary_key.rb | 2 -- .../lib/active_record/connection_adapters/schema_cache.rb | 11 ++--------- activerecord/lib/active_record/model_schema.rb | 13 +++++++------ 3 files changed, 9 insertions(+), 17 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/attribute_methods/primary_key.rb b/activerecord/lib/active_record/attribute_methods/primary_key.rb index 0560f63949..e3c7344f59 100644 --- a/activerecord/lib/active_record/attribute_methods/primary_key.rb +++ b/activerecord/lib/active_record/attribute_methods/primary_key.rb @@ -102,8 +102,6 @@ module ActiveRecord @original_primary_key = @primary_key if defined?(@primary_key) @primary_key = value && value.to_s @quoted_primary_key = nil - - connection.schema_cache.primary_keys[table_name] = @primary_key if connected? end def set_primary_key(value = nil, &block) #:nodoc: diff --git a/activerecord/lib/active_record/connection_adapters/schema_cache.rb b/activerecord/lib/active_record/connection_adapters/schema_cache.rb index bee03abd44..447e309f23 100644 --- a/activerecord/lib/active_record/connection_adapters/schema_cache.rb +++ b/activerecord/lib/active_record/connection_adapters/schema_cache.rb @@ -7,17 +7,10 @@ module ActiveRecord def initialize(conn) @connection = conn - @tables = {} + @tables = {} @columns = Hash.new do |h, table_name| - h[table_name] = - # Fetch a list of columns - conn.columns(table_name, "#{table_name} Columns").tap do |cs| - # set primary key information - cs.each do |column| - column.primary = column.name == primary_keys[table_name] - end - end + h[table_name] = conn.columns(table_name, "#{table_name} Columns") end @columns_hash = Hash.new do |h, table_name| diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb index 058161a2cb..9ed871a1cb 100644 --- a/activerecord/lib/active_record/model_schema.rb +++ b/activerecord/lib/active_record/model_schema.rb @@ -221,16 +221,16 @@ module ActiveRecord # Returns an array of column objects for the table associated with this class. def columns - if defined?(@primary_key) - connection.schema_cache.primary_keys[table_name] ||= primary_key + @columns ||= connection.schema_cache.columns[table_name].map do |col| + col = col.dup + col.primary = (col.name == primary_key) + col end - - connection.schema_cache.columns[table_name] end # Returns a hash of column objects for the table associated with this class. def columns_hash - connection.schema_cache.columns_hash[table_name] + @columns_hash ||= Hash[columns.map { |c| [c.name, c] }] end # Returns a hash where the keys are column names and the values are @@ -295,7 +295,8 @@ module ActiveRecord undefine_attribute_methods connection.schema_cache.clear_table_cache!(table_name) if table_exists? - @column_names = @content_columns = @column_defaults = @dynamic_methods_hash = @inheritance_column = nil + @column_names = @content_columns = @column_defaults = @columns = @columns_hash = nil + @dynamic_methods_hash = @inheritance_column = nil @arel_engine = @relation = nil end -- cgit v1.2.3