Skip to content

Scope with will_paginate #192

Closed
sebfie opened this Issue Oct 12, 2011 · 3 comments

3 participants

@sebfie
sebfie commented Oct 12, 2011

Hello,

I am usins scope in my controller to filter my models :

def filter
  @hotels = Hotel.scoped

  @hotels = @hotels.country(params[:country_id]) unless params[:country_id].blank?
  @hotels = @hotels.city(params[:city_id]) unless params[:city_id].blank?
  @hotels = @hotels.hoteltype(params[:hotel_type]) unless params[:hotel_type].blank?
  @hotels = @hotels.theme(params[:hotel_theme]) unless params[:hotel_theme].blank?
  @hotels = @hotels.prices(params[:hotel_prices]) unless params[:hotel_prices].blank?
  @hotels = @hotels.services(params[:hotel_services]) unless params[:hotel_services].blank?

   count = @hotels.count
   @hotels.paginate :page => params[:page], :per_page => 12
end

But it didn't work, still has all @hotels in my @hotels variable.

What did i do wrong?

Thank you.

@nicolasblanco

If you have questions about Rails or plugins, please use forums or Stackoverflow. Issues are when you can provide a failing test.

try :

   @hotels = @hotels.paginate :page => params[:page], :per_page => 12

And moreover you should use a plugin like meta_search. Remember : Thin Controller, Fat Model.

@sebfie
sebfie commented Oct 12, 2011

Thank you for help !!! An sorry to make a post on the wrong place.

@sebfie sebfie closed this Oct 12, 2011
@mislav
Owner
mislav commented Oct 12, 2011

@Ditchou, your problem is most likely in your filter code rather than pagination. Ensure that your filters work properly and, most importantly, that they don't load all records themselves! That means avoiding all() method, find(:all) or to_a calls on these scopes.

After that just perform pagination on the resulting "scope", and all should be fine. Keep an eye on your development.log to check that the generated SQL query is correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.