will_paginate with named_scopes

后端 未结 4 1971
野趣味
野趣味 2021-02-02 14:56

I\'m using will_paginate for pagination, which has been working well so far, except for this one thing.

If I try to paginate a scope, for instance

class          


        
4条回答
  •  广开言路
    2021-02-02 15:28

    Lowgain, klew's version should work out of the box. In your version you should write:

    User.scope.paginate :page => params[:page], :per_page => 10
    

    I prefer another approach to pagination. It allows to make controller more clean and encapsulates pagination at model level, for e.g.:

    class Property < ActiveRecord::Base
      named_scope :all_properties, lambda {{ :order => "name asc" }}
    
      def self.admin_properties(page = 1)
        self.all_properties.paginate(:page => page, :per_page => Settings.admin.per_page)
      end
    end
    

    And in a controller pretty clear code:

    class Admin::PropertiesController < Admin::AdminController
      def index
        @properties = Property.admin_properties(params[:page])
      end
    end
    

    p.s: Settings.admin.per_page - this is Searchlogic settings.

提交回复
热议问题