Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Active Record: fix stripping ORDER clause in paginate_by_sql

Closes #120
  • Loading branch information...
commit 370c97b092bec657f3c494f8d027a58f3245fab4 1 parent 465eda2
Daniel Teixeira authored committed
View
2  lib/will_paginate/active_record.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
View
7 spec/finders/active_record_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.