Files
forum/app/models/topic.rb
2011-08-27 13:16:04 -06:00

38 lines
942 B
Ruby

class Topic < ActiveRecord::Base
# Associations
has_many :posts, :dependent => :destroy
belongs_to :forum, :counter_cache => true
belongs_to :user, :class_name => "User", :counter_cache => true
# Accessors
attr_accessor :body
attr_accessible :title, :body, :sticky, :locked, :forum_id
# Validations
validates :title, :presence => true
validates :body, :presence => true, :on => :create
validates :posts, :presence => true, :allow_nil => false, :on => :update
validates :user, :presence => true
# Scopes
default_scope :order => 'sticky DESC, updated_at DESC'
# Callbacks
after_create :create_initial_post
# Methods
def hit!
self.class.increment_counter :hits, id
end
private
def create_initial_post
returning self.posts.build(:body => self.body) do |post|
post.forum = self.forum
post.user = self.user
post.save
end
end
end