I have something like this in my controller. Is it possible to have it
sort_by DESC instead of the default which i presume is ASC?
@type_1 = Type1.find(:all, :conditions => {:user_id => @user_id})
@type_2 = Type2.find(:all, :conditions => {:user_id => @user_id})
@type_3 = Type3.find(:all, :conditions => {:user_id => @user_id},)
@type_4 = Type4.find(:all, :conditions => {:user_id => @user_id})
@types = (@type_1 + @type_2 + @type_3 +
@type_4).sort_by(&:created_at)
--~--~---------~--~----~------------~-------~--~----~
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 Apr 8, 2008, at 1:55 PM, Keaja wrote:> > I have something like this in my controller. Is it possible to have it > sort_by DESC instead of the default which i presume is ASC? > > @type_1 = Type1.find(:all, :conditions => {:user_id => @user_id}) > @type_2 = Type2.find(:all, :conditions => {:user_id => @user_id}) > @type_3 = Type3.find(:all, :conditions => {:user_id => @user_id},) > @type_4 = Type4.find(:all, :conditions => {:user_id => @user_id}) > @types = (@type_1 + @type_2 + @type_3 + > @type_4).sort_by(&:created_at)Well, yeah: .sort_by{|t| - t.created_at.to_i} #Note the negation But why not do: class User has_many :type1s, :order => ''created_at DESC''; has_many :type2s, :order => ''created_at DESC''; has_many :type3s, :order => ''created_at DESC''; has_many :type4s, :order => ''created_at DESC''; def types (self.type1s + self.type2s + self.type3s + self.type4s).sort_by{|t| - t.created_at.to_i} end end That might not map properly to your real problem, but then you haven''t given your real code. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
@types = (@type_1, @type_2, @type_3, @type_4).sort {|a, b|
b.created_at <=> a.created_at}
On Apr 8, 3:33 pm, Rob Biedenharn
<R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org>
wrote:> On Apr 8, 2008, at 1:55 PM, Keaja wrote:
>
>
>
> > I have something like this in my controller. Is it possible to have it
> > sort_by DESC instead of the default which i presume is ASC?
>
> > @type_1 = Type1.find(:all, :conditions => {:user_id =>
@user_id})
> > @type_2 = Type2.find(:all, :conditions => {:user_id =>
@user_id})
> > @type_3 = Type3.find(:all, :conditions => {:user_id =>
@user_id},)
> > @type_4 = Type4.find(:all, :conditions => {:user_id =>
@user_id})
> > @types = (@type_1 + @type_2 + @type_3 +
> > @type_4).sort_by(&:created_at)
>
> Well, yeah:
> .sort_by{|t| - t.created_at.to_i} #Note the negation
>
> But why not do:
>
> class User
> has_many :type1s, :order => ''created_at DESC'';
> has_many :type2s, :order => ''created_at DESC'';
> has_many :type3s, :order => ''created_at DESC'';
> has_many :type4s, :order => ''created_at DESC'';
> def types
> (self.type1s + self.type2s +
> self.type3s + self.type4s).sort_by{|t| - t.created_at.to_i}
> end
> end
>
> That might not map properly to your real problem, but then you
haven''t
> given your real code.
>
> -Rob
>
> Rob Biedenharn http://agileconsultingllc.com
> R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Ahh that makes a lot of sense. I didn''t think about keeping it in the model. Thanks! On Apr 8, 2008, at 3:33 PM, Rob Biedenharn wrote: On Apr 8, 2008, at 1:55 PM, Keaja wrote:> > I have something like this in my controller. Is it possible to have it > sort_by DESC instead of the default which i presume is ASC? > > @type_1 = Type1.find(:all, :conditions => {:user_id => @user_id}) > @type_2 = Type2.find(:all, :conditions => {:user_id => @user_id}) > @type_3 = Type3.find(:all, :conditions => {:user_id => @user_id},) > @type_4 = Type4.find(:all, :conditions => {:user_id => @user_id}) > @types = (@type_1 + @type_2 + @type_3 + > @type_4).sort_by(&:created_at)Well, yeah: .sort_by{|t| - t.created_at.to_i} #Note the negation But why not do: class User has_many :type1s, :order => ''created_at DESC''; has_many :type2s, :order => ''created_at DESC''; has_many :type3s, :order => ''created_at DESC''; has_many :type4s, :order => ''created_at DESC''; def types (self.type1s + self.type2s + self.type3s + self.type4s).sort_by{|t| - t.created_at.to_i} end end That might not map properly to your real problem, but then you haven''t given your real code. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Keaja,
The find method includes an :order_by parameter. Just try:
@type_2 = Type2.find(:all, :conditions => {:user_id =>
@user_id}, :order_by => "created_at ASC")
Actually, it looks like you want to order the whole collection.
Well, you already have the whole list in the last line, so just tack
on a ".reverse" and you are all set. I.e.
@types = (@type_1 + @type_2 + @type_3 +
@type_4).sort_by(&:created_at).reverse
-Danimal
On Apr 8, 11:55 am, Keaja
<jasonmw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> I have something like this in my controller. Is it possible to have it
> sort_by DESC instead of the default which i presume is ASC?
>
> @type_1 = Type1.find(:all, :conditions => {:user_id => @user_id})
> @type_2 = Type2.find(:all, :conditions => {:user_id => @user_id})
> @type_3 = Type3.find(:all, :conditions => {:user_id => @user_id},)
> @type_4 = Type4.find(:all, :conditions => {:user_id => @user_id})
> @types = (@type_1 + @type_2 + @type_3 +
> @type_4).sort_by(&:created_at)
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Um... I think you''ll find it''s :order, not :order_by. :order_by has been deprecated for quite a while now. Julian. Learn Ruby on Rails! Check out the FREE VIDS (for a limited time) VIDEO #3 out NOW! http://sensei.zenunit.com/ On 11/04/2008, at 10:06 AM, Danimal wrote:> > Keaja, > > The find method includes an :order_by parameter. Just try: > > @type_2 = Type2.find(:all, :conditions => {:user_id => > @user_id}, :order_by => "created_at ASC") > > Actually, it looks like you want to order the whole collection. > > Well, you already have the whole list in the last line, so just tack > on a ".reverse" and you are all set. I.e. > > @types = (@type_1 + @type_2 + @type_3 + > @type_4).sort_by(&:created_at).reverse > > -Danimal > > On Apr 8, 11:55 am, Keaja <jasonmw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> I have something like this in my controller. Is it possible to have >> it >> sort_by DESC instead of the default which i presume is ASC? >> >> @type_1 = Type1.find(:all, :conditions => {:user_id => @user_id}) >> @type_2 = Type2.find(:all, :conditions => {:user_id => @user_id}) >> @type_3 = Type3.find(:all, :conditions => {:user_id => @user_id},) >> @type_4 = Type4.find(:all, :conditions => {:user_id => @user_id}) >> @types = (@type_1 + @type_2 + @type_3 + >> @type_4).sort_by(&:created_at) > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---