E
E
eldar_web2015-09-24 19:43:53
Ruby on Rails
eldar_web, 2015-09-24 19:43:53

How to get the previous post from a specific post in Ruby on Rails???

For example, we know a record with id. So, how can I find the previous entry???
Calculate from the current id-shnik - 1 and find it will not work, since such a record with such an id-shnik can be deleted.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
OnYourLips, 2015-09-24
@eldar_web

.where("id < ?", params[:id]).limit(1)
Only better not id, a created_at.

_
_ _, 2015-09-24
@AMar4enko

eldar_web depends on what you mean by "previous" entry.
If we take a blog with the possibility of delayed posting as an example, then "previous" is the first entry with an earlier publication date.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question