diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-12-16 11:46:34 +0000 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-12-16 13:52:07 +0000 |
commit | 0b08ff7d92124cc370e9f0795d1559204f04f9a4 (patch) | |
tree | 3f7e8a98b782952e12d872f20cb7603d1b014d13 /activerecord/lib | |
parent | 4fe76f4f272571bb88aa54af1d7e6bcad413c6e1 (diff) | |
download | rails-0b08ff7d92124cc370e9f0795d1559204f04f9a4.tar.gz rails-0b08ff7d92124cc370e9f0795d1559204f04f9a4.tar.bz2 rails-0b08ff7d92124cc370e9f0795d1559204f04f9a4.zip |
Cache columns at the model level.
Allows two models to use the same table but have different primary keys.
Diffstat (limited to 'activerecord/lib')
3 files changed, 9 insertions, 17 deletions
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 |