toulax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-May-01 08:56 UTC
ActiveRecord Previous/Next Record
Does ActiveRecord offer a simple solution to get previous/next records relative to a certain record? For instance, say I have this table: PEOPLE id | name 1 | john 2 | mary 3 | bob Using p = Person.find(2), is it possible to get the previous (john) and next (bob) records using perhaps something like p.previous and p.next? If not, how can I do this without resorting to ugly queries? Thanks in advance --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Try my acts_as_ordered plugin: http://svn.viney.net.nz/things/rails/plugins/acts_as_ordered/ -Jonathan. On 5/1/07, toulax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <toulax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Does ActiveRecord offer a simple solution to get previous/next records > relative to a certain record? For instance, say I have this table: > > PEOPLE > id | name > 1 | john > 2 | mary > 3 | bob > > Using p = Person.find(2), is it possible to get the previous (john) > and next (bob) records using perhaps something like p.previous and > p.next? If not, how can I do this without resorting to ugly queries? > > Thanks in advance > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
toulax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-May-01 10:59 UTC
Re: ActiveRecord Previous/Next Record
Exactly what I needed, thanks! On May 1, 6:44 am, "Jonathan Viney" <jonathan.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Try my acts_as_ordered plugin: > > http://svn.viney.net.nz/things/rails/plugins/acts_as_ordered/ > > -Jonathan. > > On 5/1/07, tou...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <tou...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Does ActiveRecord offer a simple solution to get previous/next records > > relative to a certain record? For instance, say I have this table: > > > PEOPLE > > id | name > > 1 | john > > 2 | mary > > 3 | bob > > > Using p = Person.find(2), is it possible to get the previous (john) > > and next (bob) records using perhaps something like p.previous and > > p.next? If not, how can I do this without resorting to ugly queries? > > > Thanks in advance--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Toulax,
> Using p = Person.find(2), is it possible to get the previous (john)
> and next (bob) records using perhaps something like p.previous and
> p.next? If not, how can I do this without resorting to ugly queries?
If you
- only need next()
- use MySql (optional)
simply add this in environment.rb :
class ActiveRecord::Base
def next
self.class.find(:first, :conditions => [''id >
?'', self.id])
end
end
For non-MySql db, you might have to add
:order => ''id asc''
to the query.
(see: http://weblog.jamisbuck.org/2007/4/6/faking-cursors-in-activerecord)
Alain Ravet
http://blog.ravet.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
-~----------~----~----~----~------~----~------~--~---