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 Doe". 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 -- Posted via http://www.ruby-forum.com/.
Gene Kahn 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 Doe". 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, > gkGene, 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. Regards, Michael -- Posted via http://www.ruby-forum.com/.
Hi, Thank you for your reply. 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 Modica wrote:> Gene Kahn 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 Doe". 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. > > Regards, > > Michael-- Posted via http://www.ruby-forum.com/.
Gene, 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. Sorry about that... Michael -- Posted via http://www.ruby-forum.com/.
I think what you want is something like acts_as_ordered: http://svn.viney.net.nz/things/rails/plugins/acts_as_ordered/README Regards, Michael -- Posted via http://www.ruby-forum.com/.
Michael Modica wrote:> I think what you want is something like acts_as_ordered:Wow that''s a wicked plugin. Thanks for sharing. :) - Daniel -- Posted via http://www.ruby-forum.com/.
Michael Modica wrote:> I think what you want is something like acts_as_ordered: > > http://svn.viney.net.nz/things/rails/plugins/acts_as_ordered/README > > Regards, > > MichaelLooks like what I need! Thanks for the lead. gk -- Posted via http://www.ruby-forum.com/.
>> >> http://svn.viney.net.nz/things/rails/plugins/acts_as_ordered/README >>if you don''t want/need a plugin you can send the id of the current record to your next/prev methods in the controller and then do the following: #For Next Record @foo = Foo.find(:first, :conditions => ["id > ?", params[:id]]) #For Prev Record @foo = Foo.find(:first, :conditions => ["id < ?", params[:id]]) 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
David Hughes wrote:>>> > > #For Prev Record > @foo = Foo.find(:first, :conditions => ["id < ?", params[:id]]) >Ooops - the above should include :order => ''id DESC'' at the end or else it returns the first record in the table. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2/12/08, David Hughes <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > David Hughes wrote: > >>> > > > > #For Prev Record > > @foo = Foo.find(:first, :conditions => ["id < ?", params[:id]]) > > > > Ooops - the above should include :order => ''id DESC'' at the end or else > it returns the first record in the table.And the next case should include :order => "id'' There''s really no guarantee that an unordered find will sort by primary key. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---