This is a simple, but common question I come across when dealing with people learning rails, as they inevitably do a bloggish type app first.
I assume you have a model like the one below
Adding the next/prev links
Personally, I prefer to add instance methods as well as a scope or a class method. Check it out below:
How it works
If you're confused, don't be! Let's assume we have an array of article ids:
[1,2,4,6]. If we're looking for the next article for 2, it would obviously be 4. Programmatically, you can't just say
Article.find(id+1), because it won't exist in some situations where articles are deleted.
We have to find the next id above 2, hence
where("id > ?", id). However, if you order the ids descending you'll get 6, because 6 is the highest in the list while still being larger than 2. Knowing that, we then sort the list ascending, because 4 will be first on the list when ordering ascending.
This, of course, assumes you're using the instance methods as I did, if you used
.last instead, the ordering for each would be reversed.
It's slightly faster than sorting by a date, or any other field not indexed
Since you're sorting by the primary key, it should be much faster than sorting by any other field, as the primary key is usually indexed in most databases. Plus, it's computationally easier to sort an integer, however slightly.
I ran each twice just incase some caching occurred, and the results were:
Changing the repetition will demonstrate a linear pattern, say O(n), for finding the next by, whereas by date will demonstrate a more exponential pattern, perhaps nearing O(n^2).