Search code examples
scoperuby-on-rails-3.2kaminari

how can I set a limit for a particular scope if I'm using kaminari


I'm using the kaminari gem for pagination.

I want to only show the 10 most recent items that were added. For the other scopes I can show up to 30.

Here is the index action of the Resources controller:

@filt= params[:filter] || 'no_filter'
      @resources = get_resources(params[:category]||=nil).approved.send(@filt).page(params[:page]).per(30)

Here is the scope for the recent Resources added in the Resource.rb model:

scope :recent, order('created_at DESC').limit(10)

Because the per(30) is set for the other scopes it overrides the limit in the :recent scope.

How can I only show 10 for the recent scope?


Solution

  • You can simply change the order of your function like this :

    @resources = get_resources(params[:category]||=nil).approved.page(params[:page]).per(30).send(@filt)
    

    PS : Instead this (params[:category]||=nil) you can simply do this (params[:category])

    PS2 : I think it's better to create a method class and us it like this :

    @resources = Resource.find_by_category(params[:category]).approved.page(params[:page]).per(30).send(@filt)