keaja
1
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)
keaja
2
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 B. http://agileconsultingllc.com
[email protected]
keaja
3
@types = (@type_1, @type_2, @type_3, @type_4).sort {|a, b|
b.created_at <=> a.created_at}
keaja
4
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
keaja
5
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 B. 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 B. http://agileconsultingllc.com
[email protected]
keaja
6
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/