Hi,
How would you get an SQL element, the next one and the previous one,
but the definition of a next/previous element is not dependant only of
his id but also of a status,
exemple :
yes but that doesnt resolve my problem (I think), I don't want to
select all the database, otherwise I just need to filter by status an
order by id, I just want this 3 results :-/
yes but that doesnt resolve my problem (I think), I don't want to
select all the database, otherwise I just need to filter by status an
order by id, I just want this 3 results :-/
Do you mean that you know the id of a record, and you have a defined
sort order, and you want to fetch just three records starting with the
one before the known id, for the defined sort order?
Sorry, no idea. Anyone?
You could do two finds, fetch two records starting with your central
one and the given order, then do another find of two records with the
order reversed.
You'd want to adjust the conditions to also look at status and probably take a model instance rather than an id. Maybe even a list of the attributes that must be matched (just :status in your example).
Better yet. Take these conditions and apply each as a named scope. Then, by chaining the calls you’ll get everything in one nice little query. So for example (assuming a default order of: “id ASC”):