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

paginate not working when using .order() #604

Closed
fabOnReact opened this issue Nov 14, 2019 · 1 comment
Closed

paginate not working when using .order() #604

fabOnReact opened this issue Nov 14, 2019 · 1 comment

Comments

@fabOnReact
Copy link

fabOnReact commented Nov 14, 2019

I also tried this with Kaminari and it has the same problem. My current solution is caching the total result in the frontend and performing the pagination on that data in the frontend mobile app.

This is the solution written in React

class Locations extends Component {
   componentDidMount() {
    this._setData() 
  }

  componentDidUpdate(prevProps, prevState) {
   // const next_page ...
   if(next_page) { this._setLocations() }
  }

  _setData = async () => {
    const request =  await this.api.getLocations({ flags: ["with_cameras=true"] })
    const locations = await request.json()
    this.setState({ data: locations })
  }

  get begin() {
    const { page } = this.state
    return page * 2
  }

  get end () {
    return this.begin + 2
  }

  _setLocations = async () => {
    const { data } = this.state
    const { loaded } = this.props
    const locations = data.slice(this.begin, this.end)
    this.setState({ locations })
    loaded
  }
}

I have the following Location model

class Location < ApplicationRecord
  has_many :cameras
  has_many :posts, through: :cameras
  scope :with_posts, -> { includes(cameras: :posts).where(cameras: { posts: { flagged: false }}) }
  scope :newest, -> { order(last_camera_at: :desc) }
  scope :newest_cameras, -> { order('cameras.last_post_at DESC') }
  scope :newest_posts, -> { order('posts.created_at DESC') }
end

pagination does not work

locations = Location.with_posts.newest.newest_cameras.newest_posts.paginate(page: 2, per_page: 2)
locations.size 
=> 2
locations.first.name 
=> "Dreamland"
locations = Location.with_posts.newest.newest_cameras.newest_posts.paginate(page: 3, per_page: 2)
locations.size 
=> 1
locations.first.name 
=> "Dreamland"

There is only 1 locations named "Dreamland" with multiple posts. The query returns a unique set of locations with their associated data, but will_paginate does not work after ordering the query.

associated issues
#573
#553

thanks a lot

@fabOnReact fabOnReact changed the title paginate not working when using .order() paginate not working when using .order() Nov 14, 2019
@mislav
Copy link
Owner

mislav commented Nov 14, 2019

Sorry you're having trouble! See https://github.com/mislav/will_paginate/wiki#limitations

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