class ContestMessage < ActiveRecord::Base attr_accessible :content, :contest_message_id, :contest_message_type, :status, :viewed, :user_id, :contest_id belongs_to :contest_message ,:polymorphic => true belongs_to :user belongs_to :contest has_many :message_alls, :class_name => 'MessageAll',:as =>:message, :dependent => :destroy validates :user_id, presence: true validates :contest_id, presence: true validates :contest_message_id, presence: true validates :contest_message_type, presence: true after_create :add_user_message def add_user_message if MessageAll.where("message_type = '#{self.class.to_s}' and message_id = '#{self.id}'").first.nil? && self.status != 9 self.message_alls << MessageAll.new(:user_id => self.user_id) end end end