Module: WillPaginate::ActiveRecord::BaseMethods

### Home Classes Methods

### In Files

- lib/will_paginate/active_record.rb

### Methods

- #paginate_by_sql

### Class/Module Index

Quicksearch

- WillPaginate
- WillPaginate::ActionView
- WillPaginate::ActionView::LinkRenderer
- WillPaginate::ActiveRecord
- WillPaginate::ActiveRecord::BaseMethods
- WillPaginate::ActiveRecord::Pagination
- WillPaginate::ActiveRecord::RelationMethods
- WillPaginate::Collection
- WillPaginate::CollectionMethods
- WillPaginate::DataMapper
- WillPaginate::DataMapper::CollectionMethods
- WillPaginate::DataMapper::Pagination
- WillPaginate::Deprecation
- WillPaginate::Deprecation::Hash
- WillPaginate::I18n
- WillPaginate::InvalidPage
- WillPaginate::Merb
- WillPaginate::Merb::LinkRenderer
- WillPaginate::PageNumber
- WillPaginate::PerPage
- WillPaginate::PerPage::Inheritance
- WillPaginate::Railtie
- WillPaginate::Railtie::ControllerRescuePatch
- WillPaginate::Railtie::ShowExceptionsPatch
- WillPaginate::SequelMethods
- WillPaginate::Sinatra
- WillPaginate::Sinatra::Helpers
- WillPaginate::Sinatra::LinkRenderer
- WillPaginate::ViewHelpers
- WillPaginate::ViewHelpers::LinkRenderer
- WillPaginate::ViewHelpers::LinkRendererBase
- Array
- Object

No matching classes.

# WillPaginate::ActiveRecord::BaseMethods

### Public Instance Methods

paginate_by_sql(sql, options) click to toggle source

Wraps `find_by_sql` by simply adding LIMIT and OFFSET to your SQL string based on the params otherwise used by paginating finds: `page` and `per_page`.

Example:

    @developers = Developer.paginate_by_sql ['select * from developers where salary > ?', 80000],
                           :page => params[:page], :per_page => 3

A query for counting rows will automatically be generated if you don't supply `:total_entries`. If you experience problems with this generated SQL, you might want to perform the count manually in your application.

    # File lib/will_paginate/active_record.rb, line 210
    def paginate_by_sql(sql, options)
      pagenum  = options.fetch(:page) { raise ArgumentError, ":page parameter required" } || 1
      per_page = options[:per_page] || self.per_page
      total    = options[:total_entries]

      WillPaginate::Collection.create(pagenum, per_page, total) do |pager|
        query = sanitize_sql(sql.dup)
        original_query = query.dup
        oracle = self.connection.adapter_name =~ /^(oracle|oci$)/

        # add limit, offset
        if oracle
          query =               SELECT * FROM (                SELECT rownum rnum, a.* FROM (#{query}) a                WHERE rownum <= #{pager.offset + pager.per_page}              ) WHERE rnum >= #{pager.offset}
        else
          query << " LIMIT #{pager.per_page} OFFSET #{pager.offset}"
        end

        # perfom the find
        pager.replace find_by_sql(query)

        unless pager.total_entries
          count_query = original_query.sub /\bORDER\s+BY\s+[\w`,\s.]+$/i, ''
          count_query = "SELECT COUNT(*) FROM (#{count_query})"
          count_query << ' AS count_table' unless oracle
          # perform the count query
          pager.total_entries = count_by_sql(count_query)
        end
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.