Skip to content

Commit

Permalink
Active Record: fix stripping ORDER clause in paginate_by_sql
Browse files Browse the repository at this point in the history
Closes mislav#120
  • Loading branch information
Daniel Teixeira authored and mislav committed Sep 27, 2011
1 parent 465eda2 commit 370c97b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/will_paginate/active_record.rb
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions spec/finders/active_record_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 370c97b

Please sign in to comment.