I have an ActiveRecord model object @gallery
which represents a row in the Galleries MYSQL table. Is there a way for me to ask @gallery
to give me the id to the next gallery object in the table?
The obvious way for me to do this is to :
@galleries = Gallery.find(:all)
index = @galleries.index(@gallery)
@nextgallery = @galleries[index+1]
but then I have to nilsafe this and I'm unnecessarily making another DB call to fetch all records.
Any other way out?
Best Answer
I've been in similar situation and I end up with couple solutions using Rails 3.1.3, with either class methods or scope (named_scope in Rails 2.x). This approach will work even with not sequential ids.
Class methods example using a Post model:
This could be used like:
And for scopes, using lambda should be something like: