Created
September 8, 2011 05:44
-
-
Save loss-zz/1202719 to your computer and use it in GitHub Desktop.
topic.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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