Hi,
am using this wonderful plugin acts_as_ferret and according to the
tutorial at http://railsenvy.com/2007/2/19/acts-as- … rial#basic
I worked it out except the pagination feature.
If I have 12 records and I give limit to 10, its correctly displaying 10
records in the first page and is giving the link to the second page too.
But when I go to the next page I find the same 10 records instead of the
next 2 records. Also the result count is showing only 10 instead of 12
thats the right count.
Here’s my code:
Controller:
def search
@users = User.available_users
@user = User.find(session[:user_id])
@query = params[:query] || ‘’
@total, @user_profiles = UserProfile.multi_search(@query, [ WorkProfile,
SchoolProfile ], :page => (params[:page]||1))
@pages = pages_for(@total)
unless @query.blank?
@results = UserProfile.find_by_contents @query
end
end
model:
def self.multi_search(q, additional_models = [], options = {})
return nil if q.nil? or q==“”
default_options = {:limit => 1, :page => 1}
options = default_options.merge options
# get the offset based on what page we’re on
options[:offset] = options[:limit] * (options.delete(:page).to_i-1)
# now do the query with our options
results = UserProfile.find_by_contents(q, options)
return [results.total_hits, results]
end
view:
<% if @results -%>
Your search for <%= h @query %>
returned <%= @results.size %> Results:
|
<% @results.each { |result| -%>
<%= link_to result.user.name, :controller => 'user_profiles',
:action => 'list_user', :id=> result.user %>
<% if result.user.user_pic %>
<img src=“<%=image_path url_for( :controller => “user_pics”, :action
=> “show_thumb”,
:id => result.user.user_pic ) %>” />
<% end %>
<% } -%>
<% end -%>
<%= link_to ‘Previous page’, { :page => @pages.current.previous, :query
=> @query} if @pages.current.previous %>
<%= pagination_links(@pages, :params => { :query=> @query }) %>
<%= link_to ‘Next page’, { :page => @pages.current.next, :query =>
@query} if @pages.current.next %>
Please do let me know how I get my pagination right…
Cheers
Cass
Hi Cass,
I think this is a bug in acts_as_ferret. On line 163 in
class_methods.rb the limit variable is used but hasn’t been initialised:
http://projects.jkraemer.net/acts_as_ferret/browser/trunk/plugin/acts_as_ferret/lib/class_methods.rb
In the mean time, aaf actually has paging support in there and that
looks like it should work fine.
So instead of using :limit and :offset in your options for
find_by_contents, drop all your page number calculations and
use :per_page and :page. Aaf will then figure out the limit and offset
for you.
John.
http://www.brightbox.co.uk - UK Ruby on Rails Hosting
On Wed, Oct 03, 2007 at 12:07:53PM +0100, John L. wrote:
So instead of using :limit and :offset in your options for
find_by_contents, drop all your page number calculations and
use :per_page and :page. Aaf will then figure out the limit and offset
for you.
exactly. Use the latest trunk of the plugin, and use the
find_with_ferret method with the :multi option instead of multi_search:
Model.find_with_ferret query, :page => params[:page], :per_page => 10,
:multi => [ Model2, Model3 ]
cheers,
Jens
@results = UserProfile.find_by_contents @query
options = default_options.merge options
[email protected]
http://rubyforge.org/mailman/listinfo/ferret-talk
–
Jens Krämer
http://www.jkraemer.net/ - Blog
http://www.omdb.org/ - The new free film database
Hi John and Jens,
Thanks a ton for your guidance, I got it right now after making the
changes.
Cheers
Cass
Jens K. wrote:
On Wed, Oct 03, 2007 at 12:07:53PM +0100, John L. wrote:
So instead of using :limit and :offset in your options for
find_by_contents, drop all your page number calculations and
use :per_page and :page. Aaf will then figure out the limit and offset
for you.
exactly. Use the latest trunk of the plugin, and use the
find_with_ferret method with the :multi option instead of multi_search:
Model.find_with_ferret query, :page => params[:page], :per_page => 10,
:multi => [ Model2, Model3 ]
cheers,
Jens
@results = UserProfile.find_by_contents @query
options = default_options.merge options
[email protected]
http://rubyforge.org/mailman/listinfo/ferret-talk
–
Jens Krämer
http://www.jkraemer.net/ - Blog
http://www.omdb.org/ - The new free film database
Max W. wrote:
Thanks, this has really helped me out! I have a follow-up question
though: do you know how i get the total number of search results?
Never mind, i found the answer - i just call
@search_results.total_hits
Must do more research before asking next time!
cheers
max
Jens K. wrote:
Model.find_with_ferret query, :page => params[:page], :per_page => 10,
:multi => [ Model2, Model3 ]
Thanks, this has really helped me out! I have a follow-up question
though: do you know how i get the total number of search results?
Before i started using acts_as_ferret, i was calling ‘paginate’ on a
single class. I could then use the method Collection#total_entries, but
this doesn’t work any more - my results are just being treated as an
array rather than a Collection object.
I can still use
will_paginate @search_results
to get a working set of page links, so i guess pagination is hooked up
in some places and not others.
Any idea what i’m doing wrong? thanks, max