diff options
author | lukeludwig <ludwig@tstmedia.com> | 2009-01-16 13:04:19 -0600 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2009-01-17 18:09:13 +1300 |
commit | 3ee4e009185173aab78f6503ee45e3ef4482874e (patch) | |
tree | cdf8f8219a3af0c6a33275d1217abfb4efc79d90 /activerecord/lib/active_record/associations | |
parent | fe013ce93415a88b6c23fd750bd8cbab60d6395d (diff) | |
download | rails-3ee4e009185173aab78f6503ee45e3ef4482874e.tar.gz rails-3ee4e009185173aab78f6503ee45e3ef4482874e.tar.bz2 rails-3ee4e009185173aab78f6503ee45e3ef4482874e.zip |
Cache columns for has_and_belongs_to_many associations
This avoids repeatedly calling SHOW COLUMNS when the association is queried
[#1738 state:committed]
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r-- | activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb | 12 |
1 files changed, 9 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 3d689098b5..a5cc3bf091 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 @@ -9,6 +9,14 @@ module ActiveRecord 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) options[:joins] = @join_sql @@ -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 |