I think the simplest way to attack this problem is just to write a 
paginator for an array of objects.  Then you can just do your usual 
find_by_contents, with num_docs set to :all, and then just paginate the 
array that is returned.




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

Reply via email to