To add to what Jens said, you may find this code useful:

In your model:

  def self.search(q, options = {})
    return nil if q.nil?
    default_options = {:limit => 10, :page => 1}
    options = default_options.merge options
    options[:offset] = options[:limit] * (options[:page].to_i-1)
    ... snip ...
    num = INDEX.search_each(query, {:num_docs => options[:limit],
:first_doc => options[:offset]}) do |doc, score|
    ... snip ...
    [num, results]
  end

Notice that I return the total matches as num, plus the results.  The
total matches is necessary to generate a paginator across all the
items.

For the pagination, I created this simple method in my application
controller (note it assumes a params[:page] being passed around):

  def pages_for(size, options = {})
    default_options = {:per_page => 10}
    options = default_options.merge options
    pages = Paginator.new self, size, options[:per_page], (params[:page]||1)
    pages
  end

And lastly, to use it in a controller:
  @total, @results = YourModel.search(@query, :page => (params[:page]||1)
  @result_pages = pages_for(@total)

Tom

On 5/3/06, Jens Kraemer <[EMAIL PROTECTED]> wrote:
> Hi!
>
> On Mon, May 01, 2006 at 08:55:22AM +0200, SchmakO wrote:
> > I'm just wondering where I would put the pagination for search results
> > when using "acts_as_ferret".
> >
> > At the moment my search code is..
> >
> > def search
> >     @query = params[:query] || ''
> >     unless @query.blank?
> >       @results = Tutorial.find_by_contents @query
> >     end
> > end
>
> find_by_contents has two options suitable for paging:
> :first_doc (first result to retrieve) and
> :num_docs (number of results to retrieve).
>
> so to retrieve results 10 to 20, you would use
> @results = Tutorial.find_by_contents(@query,:first_doc=>10,:num_docs=>10)
>
> hth,
> Jens
>
>
> --
> webit! Gesellschaft für neue Medien mbH          www.webit.de
> Dipl.-Wirtschaftsingenieur Jens Krämer       [EMAIL PROTECTED]
> Schnorrstraße 76                         Tel +49 351 46766  0
> D-01069 Dresden                          Fax +49 351 46766 66
> _______________________________________________
> Ferret-talk mailing list
> [email protected]
> http://rubyforge.org/mailman/listinfo/ferret-talk
>


--
Tom Davies

http://blog.atomgiant.com
http://gifthat.com

_______________________________________________
Ferret-talk mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ferret-talk

Reply via email to