Bensoussan Michael
2008-Oct-24 14:41 UTC
Sort an Array of ActiveRecords objects by created_at
Hi, I have an array of activerecords objects. I''d like to sort it by created_at column (they all have one). Do you see a smart way to do it ?? Thanks, Mike --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thorsten Müller
2008-Oct-24 14:54 UTC
Re: Sort an Array of ActiveRecords objects by created_at
what''s wrong with: @my_model = Model.find(:all, :order => "created_at ASC")) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Craig Demyanovich
2008-Oct-24 15:10 UTC
Re: Sort an Array of ActiveRecords objects by created_at
On Fri, Oct 24, 2008 at 10:59 AM, Bensoussan Michael <pada51-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> > My array is composed by different activerecord objects: > > my_model1 = Model.find(:all) > > my_model2 = Model.find(:all) > > @my_models = my_model1 + mymodel2 > > and now i''d like to sort @my_models by "created_at"@my_models = @my_models.sort_by { |m| m.created_at } Note that Enumerable#sort_by returns a new array. Regards, Craig --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Bensoussan Michael
2008-Oct-24 15:12 UTC
Re: Sort an Array of ActiveRecords objects by created_at
Yes that''s it ! ! Thank you, Mike. On Oct 24, 5:10 pm, "Craig Demyanovich" <cdemyanov...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Fri, Oct 24, 2008 at 10:59 AM, Bensoussan Michael <pad...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote: > > > > > My array is composed by different activerecord objects: > > > my_model1 = Model.find(:all) > > > my_model2 = Model.find(:all) > > > @my_models = my_model1 + mymodel2 > > > and now i''d like to sort @my_models by "created_at" > > @my_models = @my_models.sort_by { |m| m.created_at } > > Note that Enumerable#sort_by returns a new array. > > Regards, > Craig--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Maurício Linhares
2008-Oct-24 15:15 UTC
Re: Sort an Array of ActiveRecords objects by created_at
Even simpler: my_model1 = Model.find(:all) my_model2 = Model.find(:all) @my_models = (my_model1 + mymodel2).sort_by( &:created_at ) On Fri, Oct 24, 2008 at 12:12 PM, Bensoussan Michael <pada51-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Yes that''s it ! ! > > Thank you, > Mike.-- Maurício Linhares http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) João Pessoa, PB, +55 83 8867-7208 --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bensoussan Michael
2008-Oct-24 15:18 UTC
Re: Sort an Array of ActiveRecords objects by created_at
Thanks Maurício, On Oct 24, 5:15 pm, "Maurício Linhares" <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Even simpler: > > my_model1 = Model.find(:all) > my_model2 = Model.find(:all) > > @my_models = (my_model1 + mymodel2).sort_by( &:created_at ) >What the ''&'' do precisely ?> On Fri, Oct 24, 2008 at 12:12 PM, Bensoussan Michael <pad...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Yes that''s it ! ! > > > Thank you, > > Mike. > > -- > Maurício Linhareshttp://alinhavado.wordpress.com/(pt-br) |http://blog.codevader.com/(en) > João Pessoa, PB, +55 83 8867-7208--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Maurício Linhares
2008-Oct-24 15:31 UTC
Re: Sort an Array of ActiveRecords objects by created_at
Hi Michael,
The &:created_at is just a shorthand for &Proc.new { |i| i.created_at }
On Fri, Oct 24, 2008 at 12:18 PM, Bensoussan Michael
<pada51-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
> Thanks Maurício,
>
> On Oct 24, 5:15 pm, "Maurício Linhares"
<mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> wrote:
>> Even simpler:
>>
>> my_model1 = Model.find(:all)
>> my_model2 = Model.find(:all)
>>
>> @my_models = (my_model1 + mymodel2).sort_by( &:created_at )
>>
>
> What the ''&'' do precisely ?
>
>> On Fri, Oct 24, 2008 at 12:12 PM, Bensoussan Michael
<pad...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>>
>> > Yes that''s it ! !
>>
>> > Thank you,
>> > Mike.
>>
>> --
>> Maurício Linhareshttp://alinhavado.wordpress.com/(pt-br)
|http://blog.codevader.com/(en)
>> João Pessoa, PB, +55 83 8867-7208
> >
>
--
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en)
João Pessoa, PB, +55 83 8867-7208
--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Bensoussan Michael
2008-Oct-24 15:34 UTC
Re: Sort an Array of ActiveRecords objects by created_at
excellent ! Thanks for the tip. Mike On Oct 24, 5:31 pm, "Maurício Linhares" <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Michael, > > The &:created_at is just a shorthand for &Proc.new { |i| i.created_at } > > > > On Fri, Oct 24, 2008 at 12:18 PM, Bensoussan Michael <pad...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Thanks Maurício, > > > On Oct 24, 5:15 pm, "Maurício Linhares" <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > >> Even simpler: > > >> my_model1 = Model.find(:all) > >> my_model2 = Model.find(:all) > > >> @my_models = (my_model1 + mymodel2).sort_by( &:created_at ) > > > What the ''&'' do precisely ? > > >> On Fri, Oct 24, 2008 at 12:12 PM, Bensoussan Michael <pad...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> > Yes that''s it ! ! > > >> > Thank you, > >> > Mike. > > >> -- > >> Maurício Linhareshttp://alinhavado.wordpress.com/(pt-br) |http://blog.codevader.com/(en) > >> João Pessoa, PB, +55 83 8867-7208 > > -- > Maurício Linhareshttp://alinhavado.wordpress.com/(pt-br) |http://blog.codevader.com/(en) > João Pessoa, PB, +55 83 8867-7208--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bensoussan Michael
2008-Oct-24 16:01 UTC
Re: Sort an Array of ActiveRecords objects by created_at
excellent ! Thanks for the tip. Mike On Oct 24, 5:31 pm, "Maurício Linhares" <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Michael, > > The &:created_at is just a shorthand for &Proc.new { |i| i.created_at } > > > > On Fri, Oct 24, 2008 at 12:18 PM, Bensoussan Michael <pad...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Thanks Maurício, > > > On Oct 24, 5:15 pm, "Maurício Linhares" <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > >> Even simpler: > > >> my_model1 = Model.find(:all) > >> my_model2 = Model.find(:all) > > >> @my_models = (my_model1 + mymodel2).sort_by( &:created_at ) > > > What the ''&'' do precisely ? > > >> On Fri, Oct 24, 2008 at 12:12 PM, Bensoussan Michael <pad...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> > Yes that''s it ! ! > > >> > Thank you, > >> > Mike. > > >> -- > >> Maurício Linhareshttp://alinhavado.wordpress.com/(pt-br) |http://blog.codevader.com/(en) > >> João Pessoa, PB, +55 83 8867-7208 > > -- > Maurício Linhareshttp://alinhavado.wordpress.com/(pt-br) |http://blog.codevader.com/(en) > João Pessoa, PB, +55 83 8867-7208--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Oct-24 18:53 UTC
Re: Sort an Array of ActiveRecords objects by created_at
On Oct 24, 4:31 pm, "Maurício Linhares" <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Michael, > > The &:created_at is just a shorthand for &Proc.new { |i| i.created_at } >Although it should be noted that this (also known as Symbol#to_proc) is a lot slower than just writing the equivalent block (but not in ruby 1.9 or if my memory is correct, 1.8.7) Fred> > > On Fri, Oct 24, 2008 at 12:18 PM, Bensoussan Michael <pad...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Thanks Maurício, > > > On Oct 24, 5:15 pm, "Maurício Linhares" <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > >> Even simpler: > > >> my_model1 = Model.find(:all) > >> my_model2 = Model.find(:all) > > >> @my_models = (my_model1 + mymodel2).sort_by( &:created_at ) > > > What the ''&'' do precisely ? > > >> On Fri, Oct 24, 2008 at 12:12 PM, Bensoussan Michael <pad...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> > Yes that''s it ! ! > > >> > Thank you, > >> > Mike. > > >> -- > >> Maurício Linhareshttp://alinhavado.wordpress.com/(pt-br) |http://blog.codevader.com/(en) > >> João Pessoa, PB, +55 83 8867-7208 > > -- > Maurício Linhareshttp://alinhavado.wordpress.com/(pt-br) |http://blog.codevader.com/(en) > João Pessoa, PB, +55 83 8867-7208--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---