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

1

7

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

Superficial answered 24/10, 2011 at 7:56 Comment(0)
S
10

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)
Single answered 24/10, 2011 at 8:1 Comment(2)
Kaminari now provides a method to unscope pagination: 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.Brittenybrittingham
This doesn't work if you are using WillPaginate on arrays. Would be good to have a solution that works for both Relations and ArraysJudson

© 2022 - 2024 — McMap. All rights reserved.