From c0a712819c156308a9ec197361220471954d90b7 Mon Sep 17 00:00:00 2001 From: lukeludwig Date: Fri, 16 Jan 2009 13:04:19 -0600 Subject: [PATCH] Cached columns for has_and_belongs_to_many association --- .../has_and_belongs_to_many_association.rb | 12 +++++++++--- activerecord/lib/active_record/reflection.rb | 11 +++++++++++ .../has_and_belongs_to_many_associations_test.rb | 11 +++++++++++ 3 files changed, 31 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..5fc15e6 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 @@ -8,6 +8,14 @@ module ActiveRecord def create!(attributes = {}) create_record(attributes) { |record| insert_record(record, true) } end + + def columns + @reflection.columns(@reflection.options[:join_table], "#{@reflection.options[:join_table]} Columns") + end + + def reset_column_information + @reflection.reset_column_information + end protected def construct_find_options!(options) @@ -32,8 +40,6 @@ 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") - attributes = columns.inject({}) do |attrs, column| case column.name.to_s when @reflection.primary_key_name.to_s @@ -103,7 +109,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 && columns.size != 2 end private diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index dbff4f2..0fe9925 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -198,6 +198,17 @@ module ActiveRecord end end + def columns(tbl_name, log_msg) + unless defined?(@columns) && @columns + @columns = klass.connection.columns(tbl_name, log_msg) + end + @columns + end + + def reset_column_information + @columns = nil + end + def check_validity! end 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..67ad90e 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,15 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase end end end + + def test_caching_of_columns + david = Developer.find(1) + # clear cache possibly created by other tests + david.projects.reset_column_information + assert_queries(1) { david.projects.columns; david.projects.columns } + # and again to verify that reset_column_information clears the cache correctly + david.projects.reset_column_information + assert_queries(1) { david.projects.columns; david.projects.columns } + end + end -- 1.6.0.2