Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using limit and offset in addition to page #1032

Open
echan00 opened this issue Aug 21, 2020 · 1 comment
Open

Using limit and offset in addition to page #1032

echan00 opened this issue Aug 21, 2020 · 1 comment

Comments

@echan00
Copy link

echan00 commented Aug 21, 2020

@document_texts = Texts.where(active: true).reorder(created_at: :asc).limit(x).offset(y).page(params[:page])

I would like to offset my records and then limit the total number of records that can be shown with something similar to the above query. Would anybody have suggestions?

@ruurd
Copy link
Contributor

ruurd commented Dec 21, 2020

Well that probably is not going to work that way because kaminari uses OFFSET and LIMIT to do its magic. However if you know the limit and offset you want you are able to calculate the page number. Then the following will get you what you need:

p = (y / x) + 1 # pages start at 1
@document_texts = Texts.where(active: true).reorder(created_at: :asc).page(p).per(x)

Note that this works best if offset is a mulitple of limit.

BTW all of this can be found in the wiki.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants