class AppliedProject < ActiveRecord::Base
  attr_accessible :project_id, :user_id

  belongs_to :user
  belongs_to :project
  has_many :forge_messages, :class_name => 'ForgeMessage', :as => :forge_message, :dependent => :destroy

  after_create :send_appliled_message

  def send_appliled_message
    # if MessageAll.where("message_type = '#{self.class.to_s}' and message_id = '#{self.id}'").first.nil?
    self.project.members.each do |m|
      if m.roles.first.to_s.include?("Manager")
        self.forge_messages << ForgeMessage.new(:user_id => m.user_id, :project_id => self.project_id, :viewed => false)
      end
    end
    # end
  end

  #删除用户申请
  def self.deleteappiled(userid, projectid)
    @applieds = AppliedProject.where("user_id = ? and project_id = ?", userid, projectid)
    @applieds.each do |applied|
      applied.destroy
    end
  end
end
