diff --git a/lib/will_paginate/finder.rb b/lib/will_paginate/finder.rb index 6e5e15668..a32ffd3c8 100644 --- a/lib/will_paginate/finder.rb +++ b/lib/will_paginate/finder.rb @@ -140,6 +140,10 @@ def paginate_by_sql(sql, options) unless pager.total_entries count_query = original_query.sub /\bORDER\s+BY\s+[\w`,\s]+$/mi, '' count_query = "SELECT COUNT(*) FROM (#{count_query})" + + unless ['oracle', 'oci'].include?(self.connection.adapter_name.downcase) + count_query << ' AS count_table' + end # perform the count query pager.total_entries = count_by_sql(count_query) end diff --git a/test/finder_test.rb b/test/finder_test.rb index 0b60e3d80..3386c242f 100644 --- a/test/finder_test.rb +++ b/test/finder_test.rb @@ -363,7 +363,7 @@ def test_should_use_scoped_finders_if_present def test_paginate_by_sql assert_respond_to Developer, :paginate_by_sql Developer.expects(:find_by_sql).with(regexp_matches(/sql LIMIT 3(,| OFFSET) 3/)).returns([]) - Developer.expects(:count_by_sql).with('SELECT COUNT(*) FROM (sql)').returns(0) + Developer.expects(:count_by_sql).with('SELECT COUNT(*) FROM (sql) AS count_table').returns(0) entries = Developer.paginate_by_sql 'sql', :page => 2, :per_page => 3 end @@ -378,7 +378,7 @@ def test_paginate_by_sql_respects_total_entries_setting def test_paginate_by_sql_strips_order_by_when_counting Developer.expects(:find_by_sql).returns([]) - Developer.expects(:count_by_sql).with("SELECT COUNT(*) FROM (sql\n )").returns(0) + Developer.expects(:count_by_sql).with("SELECT COUNT(*) FROM (sql\n ) AS count_table").returns(0) Developer.paginate_by_sql "sql\n ORDER\nby foo, bar, `baz` ASC", :page => 2 end