From 3730ed4b5635fac4892ea60ba4fd11f41e1f22b0 Mon Sep 17 00:00:00 2001 From: lukeludwig Date: Mon, 12 Jan 2009 08:56:17 -0600 Subject: [PATCH] Cache columns for has_and_belongs_to_many associations --- .../has_and_belongs_to_many_association.rb | 4 ++-- activerecord/lib/active_record/base.rb | 11 ++++++++++- .../has_and_belongs_to_many_associations_test.rb | 12 ++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index 3d68909..6917d5f 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -32,7 +32,7 @@ module ActiveRecord if @reflection.options[:insert_sql] @owner.connection.insert(interpolate_sql(@reflection.options[:insert_sql], record)) else - columns = @owner.connection.columns(@reflection.options[:join_table], "#{@reflection.options[:join_table]} Columns") + columns = @owner.class.habtm_columns(@reflection.options[:join_table], "#{@reflection.options[:join_table]} Columns") attributes = columns.inject({}) do |attrs, column| case column.name.to_s @@ -103,7 +103,7 @@ module ActiveRecord # clause has been explicitly defined. Otherwise you can get broken records back, if, for example, the join column also has # an id column. This will then overwrite the id column of the records coming back. def finding_with_ambiguous_select?(select_clause) - !select_clause && @owner.connection.columns(@reflection.options[:join_table], "Join Table Columns").size != 2 + !select_clause && @owner.class.habtm_columns(@reflection.options[:join_table], "Join Table Columns").size != 2 end private diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index cca012e..63fa025 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1284,6 +1284,15 @@ module ActiveRecord #:nodoc: methods end end + + # Cache the columns for has_and_belongs_to_many associations by table name + def habtm_columns(table_name, name) + @habtm_columns_hash = {} unless defined?(@habtm_columns_hash) && @habtm_columns_hash + unless @habtm_columns_hash.has_key? table_name + @habtm_columns_hash[table_name] = connection.columns(table_name, name) + end + @habtm_columns_hash[table_name] + end # Resets all the cached information about columns, which will cause them # to be reloaded on the next request. @@ -1313,7 +1322,7 @@ module ActiveRecord #:nodoc: # end def reset_column_information generated_methods.each { |name| undef_method(name) } - @column_names = @columns = @columns_hash = @content_columns = @dynamic_methods_hash = @generated_methods = @inheritance_column = nil + @column_names = @columns = @columns_hash = @content_columns = @dynamic_methods_hash = @generated_methods = @inheritance_column = @habtm_columns_hash = nil end def reset_column_information_and_inheritable_attributes_for_all_subclasses#:nodoc: diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index 2f08e09..132f06c 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -775,4 +775,16 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase end end end + + uses_mocha 'mocking habtm_columns_cache' do + def test_habtm_columns_cache + Project.reset_column_information + Project.expects(:habtm_columns).times(2).with('developers_projects', 'Join Table Columns').returns(Developer.habtm_columns('developers_projects', 'whatever')) + p1 = Project.find(1) + p1.developers.find(:first) + p2 = Project.find(2) + p2.developers.find(:first) + end + end + end -- 1.6.0.2 From ea0ccd603b592b25a5d57ed082addcac9d5053d5 Mon Sep 17 00:00:00 2001 From: lukeludwig Date: Mon, 12 Jan 2009 09:05:17 -0600 Subject: [PATCH] Removing trailing whitespace --- activerecord/lib/active_record/base.rb | 2 +- .../has_and_belongs_to_many_associations_test.rb | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 63fa025..99b67c7 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1284,7 +1284,7 @@ module ActiveRecord #:nodoc: methods end end - + # Cache the columns for has_and_belongs_to_many associations by table name def habtm_columns(table_name, name) @habtm_columns_hash = {} unless defined?(@habtm_columns_hash) && @habtm_columns_hash diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index 132f06c..f18a321 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -775,7 +775,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase end end end - + uses_mocha 'mocking habtm_columns_cache' do def test_habtm_columns_cache Project.reset_column_information @@ -783,8 +783,8 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase p1 = Project.find(1) p1.developers.find(:first) p2 = Project.find(2) - p2.developers.find(:first) + p2.developers.find(:first) end end - + end -- 1.6.0.2