Index: test/finder_test.rb =================================================================== --- test/finder_test.rb (revision 432) +++ test/finder_test.rb (working copy) @@ -196,10 +196,20 @@ def test_paginate_by_sql assert_respond_to Developer, :paginate_by_sql entries = Developer.paginate_by_sql ['select * from users where salary > ?', 80000], - :page => 2, :per_page => 3, :total_entries => 9 + :page => 2, :per_page => 3 assert_equal (5..7).to_a, entries.map(&:id) + # omitting :total_entries in options returns actual count (which is 9) assert_equal 9, entries.total_entries + + # the :total_entries value should override reality (9 becomes 999) + entries = Developer.paginate_by_sql ['select * from users where salary > ?', 80000], + :page => 2, :per_page => 3, :total_entries => 999 + + # we should get the same entries back + assert_equal (5..7).to_a, entries.map(&:id) + # but the pager thinks we have a much higher total number + assert_equal 999, entries.total_entries end def test_count_by_sql Index: lib/will_paginate/finder.rb =================================================================== --- lib/will_paginate/finder.rb (revision 432) +++ lib/will_paginate/finder.rb (working copy) @@ -53,17 +53,17 @@ # :page => params[:page], :per_page => 3 # def paginate_by_sql(sql, options) - options, page, per_page = wp_parse_options!(options) + options, page, per_page, total_entries = wp_parse_options!(options) WillPaginate::Collection.create(page, per_page) do |pager| query = sanitize_sql(sql) - count_query = "SELECT COUNT(*) FROM (#{query}) AS count_table" unless options[:total_entries] + count_query = "SELECT COUNT(*) FROM (#{query}) AS count_table" unless total_entries options.update :offset => pager.offset, :limit => pager.per_page add_limit! query, options pager.replace find_by_sql(query) - pager.total_entries = options[:total_entries] || count_by_sql(count_query) unless pager.total_entries + pager.total_entries = total_entries || count_by_sql(count_query) unless pager.total_entries end end