diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2010-01-03 18:47:34 +0530 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2010-01-03 18:52:30 +0530 |
commit | 3eca0ab8388a84bf34a78395edf85e30c6943c63 (patch) | |
tree | e42a0bb826973087995fe033ae32260ea24c372d /activerecord | |
parent | d531cbc80959dfc35ee1dcbac133d52d63beacc9 (diff) | |
download | rails-3eca0ab8388a84bf34a78395edf85e30c6943c63.tar.gz rails-3eca0ab8388a84bf34a78395edf85e30c6943c63.tar.bz2 rails-3eca0ab8388a84bf34a78395edf85e30c6943c63.zip |
Give preference to the second relation's order when merging
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 13 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 1 |
2 files changed, 10 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 114095b7ef..9cfd9b6d23 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -38,13 +38,16 @@ module ActiveRecord merged_relation = merged_relation. joins(arel.joins(arel)). group(arel.groupings). - order(arel.send(:order_clauses).join(', ')). limit(arel.taken). offset(arel.skipped). select(arel.send(:select_clauses)). from(arel.sources) end + relation_order = r.send(:order_clause) + merged_order = relation_order.present? ? relation_order : order_clause + merged_relation = merged_relation.order(merged_order) + merged_wheres = @relation.wheres r.wheres.each do |w| @@ -83,7 +86,7 @@ module ActiveRecord :select => @relation.send(:select_clauses).join(', '), :joins => @relation.joins(relation), :group => @relation.send(:group_clauses).join(', '), - :order => @relation.send(:order_clauses).join(', '), + :order => order_clause, :conditions => where_clause, :limit => @relation.taken, :offset => @relation.skipped, @@ -156,7 +159,7 @@ module ActiveRecord end def reset - @first = @last = @create_scope = @to_sql = nil + @first = @last = @create_scope = @to_sql = @order_clause = nil @records = [] self end @@ -219,6 +222,10 @@ module ActiveRecord @relation.send(:where_clauses).join(join_string) end + def order_clause + @order_clause ||= @relation.send(:order_clauses).join(', ') + end + def references_eager_loaded_tables? joined_tables = (tables_in_string(@relation.joins(relation)) + [table.name, table.table_alias]).compact.uniq (tables_in_string(to_sql) - joined_tables).any? diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 18f6152cc0..7046420ebd 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -539,5 +539,4 @@ class RelationTest < ActiveRecord::TestCase assert ! hen.new_record? assert_equal 'hen', hen.name end - end |