In my app, a user has_many resolutions, and i want @user.resolutions to return only the most recently updated five resolutions, but ordered *oldest first* out of those five. So, let''s say i have 12 records, which we''ll label 1 through to 12. For simplicity''s sake let''s say that the order of updated_at matches the order of the labels. Then, has_many :resolutions, :order => "id DESC", :limit => 5 gives me [12,11,10,9,8] This *is* the most recent five, but i want them ordered the other way, ie [8,9,10,11,12] How do i do this in the has_many method call? thanks max -- 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 -~----------~----~----~----~------~----~------~--~---
Nathan Esquenazi
2007-Dec-19 21:27 UTC
Re: Make has_many return only the latest 5 *in order*
Couldn''t you just do [12,11,10,9,8].reverse ? -- 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 -~----------~----~----~----~------~----~------~--~---
Nathan Esquenazi wrote:> Couldn''t you just do [12,11,10,9,8].reverse ?I want to avoid writing .reverse all over my controllers though - i''d like the ordering to be automatic. -- 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 -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom wrote:>> Nathan Esquenazi wrote: >>> Couldn''t you just do [12,11,10,9,8].reverse ? >> >> I want to avoid writing .reverse all over my controllers though - i''d >> like the ordering to be automatic. > > In your model... > > def reversed_resolutions > resolutions.reverse > end > > > Then you can just reference that.Again, i don''t want to write reversed_resolutions all over my controllers. It''s unDRY and liable to introduce errors. -- 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 -~----------~----~----~----~------~----~------~--~---
def resolutions Resolution.find_by_user_id(id, :order => "id DESC", :limit => 5).reverse end If you''re not willing to accept people''s answers then don''t ask questions. On Dec 20, 2007 8:56 AM, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Philip Hallstrom wrote: > >> Nathan Esquenazi wrote: > >>> Couldn''t you just do [12,11,10,9,8].reverse ? > >> > >> I want to avoid writing .reverse all over my controllers though - i''d > >> like the ordering to be automatic. > > > > In your model... > > > > def reversed_resolutions > > resolutions.reverse > > end > > > > > > Then you can just reference that. > > Again, i don''t want to write reversed_resolutions all over my > controllers. It''s unDRY and liable to introduce errors. > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg http://www.frozenplague.net --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom
2007-Dec-20 00:23 UTC
Re: Make has_many return only the latest 5 *in order*
> Nathan Esquenazi wrote: >> Couldn''t you just do [12,11,10,9,8].reverse ? > > I want to avoid writing .reverse all over my controllers though - i''d > like the ordering to be automatic.In your model... def reversed_resolutions resolutions.reverse end Then you can just reference that.> -- > 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 -~----------~----~----~----~------~----~------~--~---
Ryan Bigg wrote:> def resolutions > Resolution.find_by_user_id(id, :order => "id DESC", :limit => > 5).reverse > end > > If you''re not willing to accept people''s answers then don''t ask > questions. >> Ryan Bigg > http://www.frozenplague.netThanks Ryan. Sorry to those earlier posters, i didn''t mean to sound rude (just comes natural to an ass like me). I actually tried the above, ie replacing has_many with a regular instance method, but then i found i was having problems doing stuff like this @user.resolutions << resolution Ie it adds the new resolution to the local returned array from the method, but not to the database. thanks again, max -- 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 -~----------~----~----~----~------~----~------~--~---
If you want that functionality then you''re going to have to call .reverse in your controllers... or you could do something extremely hackish: alias_method :associated_resolutions, :resolutions def resolutions associated_resolutions.reverse end This should still allow you to have the << and = functionality with resolutions, but allow you to display them in reverse order. Put the order and the limit back onto the has_many. I don''t know why I didn''t think of it before. On Dec 20, 2007 7:55 PM, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Ryan Bigg wrote: > > def resolutions > > Resolution.find_by_user_id(id, :order => "id DESC", :limit => > > 5).reverse > > end > > > > If you''re not willing to accept people''s answers then don''t ask > > questions. > > > > > Ryan Bigg > > http://www.frozenplague.net > > Thanks Ryan. Sorry to those earlier posters, i didn''t mean to sound > rude (just comes natural to an ass like me). > > I actually tried the above, ie replacing has_many with a regular > instance method, but then i found i was having problems doing stuff like > this > > @user.resolutions << resolution > > Ie it adds the new resolution to the local returned array from the > method, but not to the database. > > thanks again, max > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg http://www.frozenplague.net --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ryan Bigg wrote:> If you want that functionality then you''re going to have to call > .reverse in > your controllers... or you could do something extremely hackish: > alias_method :associated_resolutions, :resolutions > def resolutions > associated_resolutions.reverse > end > > This should still allow you to have the << and = functionality with > resolutions, but allow you to display them in reverse order. Put the > order > and the limit back onto the has_many. I don''t know why I didn''t think of > it > before. > > On Dec 20, 2007 7:55 PM, Max Williams <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > >> >> @user.resolutions << resolution >> >> Ie it adds the new resolution to the local returned array from the >> method, but not to the database. >> >> thanks again, max >> -- >> Posted via http://www.ruby-forum.com/. >> >> > >> > > > -- > Ryan Bigg > http://www.frozenplague.netah - does that mean that "associated_whatevers" is an under the hood rails method used when we do stuff like @user.resolutions << resolution ? ie something that''s set up by ''has_many''? -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Dec-20 12:00 UTC
Re: Make has_many return only the latest 5 *in order*
On 19 Dec 2007, at 21:23, Max Williams wrote:> > In my app, a user has_many resolutions, and i want @user.resolutions > to > return only the most recently updated five resolutions, but ordered > *oldest first* out of those five. > > So, let''s say i have 12 records, which we''ll label 1 through to 12. > For > simplicity''s sake let''s say that the order of updated_at matches the > order of the labels. Then, > > has_many :resolutions, :order => "id DESC", :limit => 5 >A bit nasty, but how about has_many :resolutions, :finder_sql => ''SELECT * from (SELECT * from resolutions order by id desc limit 5) as t order by id asc'' Fred>
Frederick Cheung wrote:> On 19 Dec 2007, at 21:23, Max Williams wrote: > >> >> has_many :resolutions, :order => "id DESC", :limit => 5 >> > > A bit nasty, but how about > > has_many :resolutions, :finder_sql => ''SELECT * from (SELECT * from > resolutions order by id desc limit 5) as t order by id asc'' > > Fredah i never thought about simply doing it all in sql :) cheers -- 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 -~----------~----~----~----~------~----~------~--~---
associated_resolutions is what alias_method duplicates resolutions into. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ryan Bigg wrote:> associated_resolutions is what alias_method duplicates resolutions into.Ah right, i see what you mean. :) thanks. -- 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 -~----------~----~----~----~------~----~------~--~---