
Untitled
By: a guest on
May 10th, 2012 | syntax:
None | size: 0.61 KB | hits: 17 | expires: Never
Ordering by a two fields, one being created_at, In Rails
Model.order('popularity ASC')
Model.order('popularity-integerize(created_at) ASC')
class Model < ActiveRecord::Base
def decaying_popularity
popularity + created_at.to_i # created_at is larger for newer creations.
end # you'll obviously need to edit the formula you use.
def self.order_by_decaying_popularity
models = self.all
models.sort {|a,b| a.decaying_popularity <=> b.decaying_popularity }
end
end
Model.order_by_decaying_popularity
Model.where(:author_id => 1).order_by_decaying_popularity