The snipped out portion is just where you process the results.  In my
case, I am searching for gifts so here is how I search and collect the
Gift objects:

    num = INDEX.search_each(query, {:num_docs => options[:limit],
:first_doc => options[:offset]}) do |doc, score|
      logger.debug("Found doc: #{doc}, id: #{INDEX[doc]['id']}, score:
#{score}")
      gifts << Gift.find(INDEX[doc]['id'])
    end

Also, the reason I defined my own search method as opposed to using
find_by_contents is because I am not using the acts_as_ferret plugin.
>From looking at the acts_as_ferret code, it looks like you can just
use the SearchResults object returned by the find_by_contents since it
also includes the total hits needed to create the Paginator pages.

Tom

On 7/5/06, guest <[EMAIL PROTECTED]> wrote:
>
> Nice!
>
> This is very useful!
>
> What code did you snip out of the serach method? Is that required?
>
> Thanks.
>
>
> Tom Davies wrote:
> > 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:
> >> >     unless @query.blank?
> >>
> >> Ferret-talk mailing list
> >> [email protected]
> >> http://rubyforge.org/mailman/listinfo/ferret-talk
> >>
> >
> >
> > --
> > Tom Davies
> >
> > http://blog.atomgiant.com
> > http://gifthat.com
>
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Ferret-talk mailing list
> [email protected]
> http://rubyforge.org/mailman/listinfo/ferret-talk
>


-- 
Tom Davies

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

Reply via email to