Finding the next record in the database with Active Record

Posted by ericraio on Stack Overflow See other posts from Stack Overflow or by ericraio
Published on 2011-11-20T01:27:38Z Indexed on 2011/11/20 1:52 UTC
Read the original article Hit count: 217

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby