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: <div id="header"> <% if @results -%> <div id="result"> <table cellpadding="1" cellspacing="1" width="auto"> <tr valign="top" class="resultdetails"> <td>Your search for <font size="3"><b><%= h @query %></b></font> returned <font size="3"><b><%= @results.size %></b></font> Results:</p> <ul> </td> </tr> </table> <% @results.each { |result| -%> <li><%= link_to result.user.name, :controller => ''user_profiles'', :action => ''list_user'', :id=> result.user %></li><br /> <% if result.user.user_pic %> <img src="<%=image_path url_for( :controller => "user_pics", :action => "show_thumb", :id => result.user.user_pic ) %>" /> <% end %> <% } -%> </ul> <br /> <% end -%> </div> </div> <%= 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 -- Posted via http://www.ruby-forum.com/.
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, 2007-10-03 at 12:52 +0200, Cass Amino wrote:> 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
On Wed, Oct 03, 2007 at 12:07:53PM +0100, John Leach wrote:> 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.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> > John. > -- > http://www.brightbox.co.uk - UK Ruby on Rails Hosting > > On Wed, 2007-10-03 at 12:52 +0200, Cass Amino wrote: > > 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 > > > _______________________________________________ > Ferret-talk mailing list > Ferret-talk at rubyforge.org > 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 Kraemer wrote:> On Wed, Oct 03, 2007 at 12:07:53PM +0100, John Leach 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 >> Ferret-talk at rubyforge.org >> http://rubyforge.org/mailman/listinfo/ferret-talk > -- > Jens Kr?mer > http://www.jkraemer.net/ - Blog > http://www.omdb.org/ - The new free film database-- Posted via http://www.ruby-forum.com/.