class AppliedMessage < ActiveRecord::Base
  # status: 0表示未批准； status：1表示已批准； status： 2表示已拒绝

  attr_accessible :applied_id, :applied_type, :status, :user_id, :viewed, :applied_user_id, :role, :project_id, :name
  belongs_to :applied ,:polymorphic => true
  belongs_to :apply_add_schools
  belongs_to :user
  has_many :message_alls, :class_name => 'MessageAll', :as =>:message,  :dependent => :destroy

  validates :user_id,presence: true
  validates :applied_id,presence: true
  validates :applied_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.message_alls << MessageAll.new(:user_id => self.user_id, :viewed => false)
    end
  end

end
