From 71bdf13b60bb8ac6a06f05eea94399bf9de27a92 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 13 Apr 2005 17:51:43 +0000 Subject: Removed the default order by id on has_and_belongs_to_many queries as it could kill performance on large sets (you can still specify by hand with :order) git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1162 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../has_and_belongs_to_many_association.rb | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'activerecord/lib/active_record/associations') 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 eda6992243..85f4122138 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 @@ -7,7 +7,7 @@ module ActiveRecord @association_foreign_key = options[:association_foreign_key] || Inflector.underscore(Inflector.demodulize(association_class_name)) + "_id" @association_table_name = options[:table_name] || @association_class.table_name @join_table = options[:join_table] - @order = options[:order] || "t.#{@association_class.primary_key}" + @order = options[:order] construct_sql end @@ -146,12 +146,18 @@ module ActiveRecord def construct_sql interpolate_sql_options!(@options, :finder_sql, :delete_sql) - @finder_sql = @options[:finder_sql] || - "SELECT t.*, j.* FROM #{@join_table} j, #{@association_table_name} t " + - "WHERE t.#{@association_class.primary_key} = j.#{@association_foreign_key} AND " + - "j.#{@association_class_primary_key_name} = #{@owner.quoted_id} " + - (@options[:conditions] ? " AND " + interpolate_sql(@options[:conditions]) : "") + " " + - "ORDER BY #{@order}" + + if @options[:finder_sql] + @finder_sql = @options[:finder_sql] + else + @finder_sql = + "SELECT t.*, j.* FROM #{@join_table} j, #{@association_table_name} t " + + "WHERE t.#{@association_class.primary_key} = j.#{@association_foreign_key} AND " + + "j.#{@association_class_primary_key_name} = #{@owner.quoted_id} " + + @finder_sql << " AND #{interpolate_sql(@options[:conditions])}" if @options[:conditions] + @finder_sql << "ORDER BY #{@order}" if @order + end end end end -- cgit v1.2.3