Permalink
Browse files

Active Record: fix stripping ORDER clause in paginate_by_sql

Closes #120
  • Loading branch information...
1 parent 465eda2 commit 370c97b092bec657f3c494f8d027a58f3245fab4 Daniel Teixeira committed with Aug 7, 2011
Showing with 6 additions and 3 deletions.
  1. +1 −1 lib/will_paginate/active_record.rb
  2. +5 −2 spec/finders/active_record_spec.rb
@@ -177,7 +177,7 @@ def paginate_by_sql(sql, options)
pager.replace find_by_sql(query)
unless pager.total_entries
- count_query = original_query.sub /\bORDER\s+BY\s+[\w`,\s]+$/mi, ''
+ count_query = original_query.sub /\bORDER\s+BY\s+[\w`,\s.]+$/mi, ''
count_query = "SELECT COUNT(*) FROM (#{count_query})"
unless self.connection.adapter_name =~ /^(oracle|oci$)/i
@@ -228,12 +228,15 @@
it "should strip the order when counting" do
lambda {
- sql = "select id, title, content from topics order by title"
+ sql = "select id, title, content from topics order by topics.title"
topics = Topic.paginate_by_sql sql, :page => 1, :per_page => 2
topics.first.should == topics(:ar)
}.should run_queries(2)
+
+ $query_sql.last.should include('COUNT')
+ $query_sql.last.should_not include('order by topics.title')
end
-
+
it "shouldn't change the original query string" do
query = 'select * from topics where 1 = 2'
original_query = query.dup

0 comments on commit 370c97b

Please sign in to comment.