Skip to content

Instantly share code, notes, and snippets.

@loss-zz
Created September 8, 2011 05:44
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 loss-zz/1202719 to your computer and use it in GitHub Desktop.
Save loss-zz/1202719 to your computer and use it in GitHub Desktop.
topic.rb
# encoding: utf-8
class Topic < ActiveRecord::Base
include Redis::Objects
#Redis::Objects
counter :hits #利用redis存储点击
belongs_to :user
belongs_to :node
has_many :posts
before_save :set_last_updated_at
after_create :rest_posts_count
accepts_nested_attributes_for :posts, :reject_if => lambda { |a| a[:body].blank? }, :allow_destroy => true
scope :recent, order('last_updated_at DESC')
def body
return self.posts.major.first.body if self.posts.major.first
''
end
def last_reply
Post.find(self.last_post_id) if self.last_post_id
end
def update_cached_post_fields(post)
self.class.update_all(['last_post_id = ?', post.id], ['id = ?', id])
end
def set_last_updated_at
self.last_updated_at = Time.now
end
def rest_posts_count
self.class.update_all(['posts_count = ?', 0], ['id = ?', self.id])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment