I’m trying to look up 1 instance of a model via a slug, which I can do via
@project = Project.where(:slug => params[:id]).first
But once I get the instance I want to find the next and previous instances of the model from a order of a column called publish which is a date.
how would I get the slug only of the previous and next instance of my arbitrary order by column?
::Edit::
Adding this to my modeled worked.
def previous(offset = 0)
self.class.select('slug').first(:conditions => ['published < ?', self.id], :limit => 1, :offset => offset, :order => "published DESC")
end
def next(offset = 0)
self.class.select('slug').first(:conditions => ['published > ?', self.id], :limit => 1, :offset => offset, :order => "published ASC")
end
I found this solution at Next, Previous Records Using Named Scope
Assuming your
publishdates are unique something like this ought to work:For
@prev_projectthewhereclause indicates all Projects before@project, theorderlists them latest first (which puts the one right before@projectat the top) andfirstlimits the query to one result, i.e. the previous Project. The query for@next_projectis the same but reversed. Ifpublishisn’t unique you’ll have to sort on a second criterium as well.However, you may want to consider not reinventing the wheel and using the very common gem acts_as_list, which would make this as simple as
@project.higher_itemand@project.lower_item.