Skip to content

Instantly share code, notes, and snippets.

@caike
Created January 23, 2011 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caike/791826 to your computer and use it in GitHub Desktop.
Save caike/791826 to your computer and use it in GitHub Desktop.
AR call back with conditional
class Topic < ActiveRecord::Base
before_create :set_start_trending
before_save :set_hot_topic, :if => Proc.new { |topic| topic.mentions >= HOT_TOPIC_MENTIONS }
scope :trending_top, lambda { |num| where("started_trending > ?", 1.day.ago).order('mentions desc').limit(num) }
HOT_TOPIC_MENTIONS = 1000
def to_param
"#{id}-#{name.parameterize}"
end
private
def set_start_trending
self.started_trending = Time.now
end
def set_hot_topic
self.hot_topic = true #if self.mentions > HOT_TOPIC_MENTIONS
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment