6

Let's say we have @posts = Post.published.per(10).page(params[:page]) somewhere in our controller. Later on we need to update all published posts (Post.published).

How should one remove the pagination?

@posts.limit(false).offset(false) seems to do the trick but I think there should be some other way, a more high-level way (something like @posts.without_pagination).

Kostas
  • 8,356
  • 11
  • 47
  • 63

1 Answers1

9

As the page method is provided by kaminari, I do not sure if kaminari provide also something like without_pagination.

But if you like, you could always do this:

class Post < ActiveRecord::Base
  def self.without_pagination
    self.limit(false).offset(false)
  end
end

or you could extract the method out to a module and include the module into ActiveRecord::Base so that every model has the method.

Edited

Thank you tfwright for pointing out. Could use .except(:limit, :offset) which should be much better for later comers.

@posts = Post.publised.per(10).page(params[:page])
@posts = @posts.except(:limit, :offset)
PeterWong
  • 15,951
  • 9
  • 59
  • 68
  • 1
    Kaminari now provides a method to unscope pagination: https://github.com/kaminari/kaminari#unscoping. I'm not sure if it currently behaves any differently, but it might be more future proof to use it rather than overriding the AR options directly. – tfwright Sep 05 '18 at 19:25
  • This doesn't work if you are using WillPaginate on arrays. Would be good to have a solution that works for both Relations and Arrays – Peter P. Sep 01 '19 at 21:16