Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 10th, 2012  |  syntax: None  |  size: 0.61 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Ordering by a two fields, one being created_at, In Rails
  2. Model.order('popularity ASC')
  3.        
  4. Model.order('popularity-integerize(created_at) ASC')
  5.        
  6. class Model < ActiveRecord::Base
  7.   def decaying_popularity
  8.     popularity + created_at.to_i # created_at is larger for newer creations.
  9.   end                            # you'll obviously need to edit the formula you use.
  10.  
  11.   def self.order_by_decaying_popularity
  12.     models = self.all
  13.     models.sort {|a,b| a.decaying_popularity <=> b.decaying_popularity }
  14.   end
  15. end
  16.        
  17. Model.order_by_decaying_popularity
  18.        
  19. Model.where(:author_id => 1).order_by_decaying_popularity