0

I have a posts model with an integer column called 'position' to manually order them. On a post's show page I have a 'more posts' section with @more_posts.

@more_posts = @comment.posts.where("posts.id NOT IN (?)", @post.id).order("position").limit(2) 

This gives me two unique posts but I'm not sure how to get the next two posts instead of the two highest positioned posts. Say the current post.id is 2, I'd like @more_posts to get posts 3 and 4.

(I'm using postgresql is production and sqlite in development.)

user2759575
  • 553
  • 3
  • 25
  • What is your DB client name? Mysql? – Arup Rakshit May 25 '15 at 18:54
  • possible duplicate of [Rails best way to get previous and next active record object](http://stackoverflow.com/questions/25665804/rails-best-way-to-get-previous-and-next-active-record-object), which I incidentally answered. – D-side May 25 '15 at 19:19

1 Answers1

0
@more_posts = @comment.posts.where(id: [@post.id + 1, @post.id + 2])

or

@more_posts = @comment.posts.where("id > ?", @post.id).limit(2)

If you are ordering by position consider:

@more_posts = @comment.posts.order(position: :asc).where("position > ?", @post.position).limit(2)
gabrielhilal
  • 10,660
  • 6
  • 54
  • 81
  • Neither seem to work and return nothing. Since I'm ordering by position and not id I switched those like so: @comment.posts.where("position > ?", @post.position).limit(2) – user2759575 May 25 '15 at 19:55