Finding the next record in the database with Active Record
- by ericraio
So I have my rails application and I have blog posts in my application.
For starters I am on rails 2.3.5 and Ruby 1.8.7
For the show page, I am required to give a prev/next link to the prev/next blog post.
The catch is that I need to find the next blog where the language column in the database is equal to 'eng'. I had started writing this out in my model and it works but of course this will just find the prev/next record in the database no matter what the language is specified in the column and it will break when the record is not found.
def next(lang='eng')
BlogEntry.find(self.id - 1)
end
def prev(lang='eng')
BlogEntry.find(self.id + 1)
end