Hello,
Is there some built-in facility for fetching the Previous or Next record
from a table? Assume the command find_by_name has been issued, and the
variable @name contains the name “John D.”. I’m looking for something
like find_next(@name) and find_previous(@name). What’s a good way to
pursue if one has to roll their own methods?
Hello,
Is there some built-in facility for fetching the Previous or Next record
from a table? Assume the command find_by_name has been issued, and the
variable @name contains the name “John D.”. I’m looking for something
like find_next(@name) and find_previous(@name). What’s a good way to
pursue if one has to roll their own methods?
Thanks for the help,
gk
Gene,
I have done something very similar by using pagination and setting
per_page = 1 . Then, when I reference the first item, I use @model[0]
to get the details of the item. For example, if my model is leads, then
I use @lead = @leads[0]
This allows me to use a detail or edit view instead of a list view. Not
really sure if this is the best approach or not, but it is the way I do
it.
Alternatively, you could build the SQL and use an offset. Just keep the
offset stored somewhere so you can decrement or increment as necessary.
I’m sure others may have a better answer but I at least wanted to
contribute an answer that works for me.
The Previous and Next functions are to be built into the Show view. The
Show view when triggered is supplied the id of the record to show, as
per the default Show behavior:
def show @eng_lemma = EngLemma.find(params[:id])
end
While viewing a record in Show, this is where the Previous and Next
functions are needed, as well as a Find function. Since Show retrieves a
list of one record only, wouldn’t incrementing or decrementing the @model[i] index yield nothing?
I’m just not understanding it.
Thanks again,
gk
Michael M. wrote:
Gene K. wrote:
Hello,
Is there some built-in facility for fetching the Previous or Next record
from a table? Assume the command find_by_name has been issued, and the
variable @name contains the name “John D.”. I’m looking for something
like find_next(@name) and find_previous(@name). What’s a good way to
pursue if one has to roll their own methods?
Thanks for the help,
gk
Gene,
I have done something very similar by using pagination and setting
per_page = 1 . Then, when I reference the first item, I use @model[0]
to get the details of the item. For example, if my model is leads, then
I use @lead = @leads[0]
This allows me to use a detail or edit view instead of a list view. Not
really sure if this is the best approach or not, but it is the way I do
it.
Alternatively, you could build the SQL and use an offset. Just keep the
offset stored somewhere so you can decrement or increment as necessary.
I’m sure others may have a better answer but I at least wanted to
contribute an answer that works for me.
My solution won’t work for you I don’t think! Now that you explained it
further, my sample is invalid. You want that functionality from ANY
show view - whereas I needed to show a list of records in a show/edit
manner and navigate between them. In the solution I provided, you
wouldn’t be able to go to a list, select an item, go to show, and then
from show get the next/previous record.
Maybe someone else will be able to come up with a solution for you.
If you reach the end or start of the record list you can do a redirect
or something more creative depending on your needs. This will work with
dates, string and integer fields as well.