By Peter Cooper / June 6, 2006
Pagination in Rails is good, but it can lack flexibility in many situations. At that point it’s time to roll your own. However, Phil Bogle and Laurel Fan came up with a solution they call paginate_by_sql that can solve some of the custom pagination problems. This needs to become a plugin.
I’m reposting their code here simply because I like to see syntax coloring :) Read More
# paginate by sql
# http://thebogles.com/blog/2006/06/paginate_by_sql-for-rails-a-more-general-approach/
# added support for sql with arguments
# added a :count option for passing in either a Integer count or count query.
module ActiveRecord
class Base
def self.find_by_sql_with_limit(sql, offset, limit)
sql = sanitize_sql(sql)
add_limit!(sql, {:limit => limit, :offset => offset})
find_by_sql(sql)
end
def self.count_by_sql_wrapping_select_query(sql)
sql = sanitize_sql(sql)
count_by_sql("select count(*) from (#{sql})")
end
end
end
class ApplicationController < ActionController::Base
def paginate_by_sql(model, sql, per_page, options={})
if options[:count]
if options[:count].is_a?