Search code examples
rubyruby-on-rails-3kaminari

Is there a way to remove the pagination from an ActiveRelation object?


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).


Solution

  • 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)