ruby on rails - How do you retrieve k prior posts before an id in ActiveRecord -


i trying infinite scrolling in reverse show recent 20 posts initially. user scrolls up, want retrieve (in rails) next 20 recent posts, etc...

the following code it, gives list of posts in reverse order due "order":

room.posts.where('id < ?', id).order('created_at desc').limit(20) 

i posts in ascending order. reverse array after retrieval, seems waste of computation.

on other hand, if leave "order" out (as shown below), wrong cause i'd 20 earliest posts rather 20 recent.

room.posts.where('id < ?', id).limit(20) 

i'm hoping understand problem correctly. how about:

room.posts.where('id < ?', id).order('created_at desc').limit(20).reverse 

the reverse @ end reverses order of results in query. if you're trying avoid, i'm not sure how else you'd it. seems pretty non-intensive way done.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -