diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-04-13 17:51:43 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-04-13 17:51:43 +0000 |
commit | 71bdf13b60bb8ac6a06f05eea94399bf9de27a92 (patch) | |
tree | 1a78e733795b5cd5c7eb9738313440365e416288 /activerecord | |
parent | 54cc595dff2564d6cb48c40709637170bca79837 (diff) | |
download | rails-71bdf13b60bb8ac6a06f05eea94399bf9de27a92.tar.gz rails-71bdf13b60bb8ac6a06f05eea94399bf9de27a92.tar.bz2 rails-71bdf13b60bb8ac6a06f05eea94399bf9de27a92.zip |
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
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 |