Skip to content

Instantly share code, notes, and snippets.

@lifo
Created August 29, 2010 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lifo/556759 to your computer and use it in GitHub Desktop.
Save lifo/556759 to your computer and use it in GitHub Desktop.
diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb
index e7e5f26..61cac59 100644
--- a/activerecord/lib/active_record/relation/spawn_methods.rb
+++ b/activerecord/lib/active_record/relation/spawn_methods.rb
@@ -6,7 +6,7 @@ module ActiveRecord
merged_relation = clone
return merged_relation unless r
- ((Relation::ASSOCIATION_METHODS + Relation::MULTI_VALUE_METHODS) - [:joins, :where]).each do |method|
+ ((Relation::ASSOCIATION_METHODS + Relation::MULTI_VALUE_METHODS) - [:joins, :where, :order]).each do |method|
value = r.send(:"#{method}_values")
unless value.empty?
if method == :includes
@@ -17,6 +17,10 @@ module ActiveRecord
end
end
+ if (order_values = r.order_values).present?
+ merged_relation.order_values = order_values + merged_relation.order_values
+ end
+
merged_relation = merged_relation.joins(r.joins_values)
merged_wheres = @where_values
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index aa75aa2..d9fdb10 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -667,4 +667,8 @@ class RelationTest < ActiveRecord::TestCase
def test_relations_limit_with_conditions_or_limit
assert_equal Post.limit(2).size, Post.limit(2).all.size
end
+
+ def test_chaining_scopes_using_order_method_is_equivalent_to_chaining_scopes_using_scope_with_that_order
+ assert_equal Post.order("id").order("comments_count DESC").order_values, Post.order("id").ranked_by_comments.order_values
+ end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment