class MemoMessage < ActiveRecord::Base
  attr_accessible :forum_id, :memo_id, :memo_type, :user_id, :viewed

  belongs_to :memo
  belongs_to :user
  has_many :message_alls, :class_name => 'MessageAll',:as =>:message,  :dependent => :destroy

  validates :user_id,presence: true
  validates :forum_id,presence: true
  validates :memo_id,presence: true
  validates :memo_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)
    end
  end
end
