diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb | 20 |
2 files changed, 15 insertions, 7 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 810fc94d97..217413999e 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* 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) + * Fixed that Base.silence should restore the old logger level when done, not just set it to DEBUG #1084 [yon@milliped.com] * Fixed boolean saving on Oracle #1093 [mparrish@pearware.org] 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 |