socialforge/app/models/contest_activity.rb

38 lines
1.4 KiB
Ruby

class ContestActivity < ActiveRecord::Base
attr_accessible :contest_act_id, :contest_act_type, :user_id, :contest_id
belongs_to :user
belongs_to :contest
belongs_to :contest_act ,:polymorphic => true
has_many :user_acts, :class_name => 'UserAcivity',:as =>:act
after_create :add_user_activity
before_destroy :destroy_user_activity
#在个人动态里面增加当前动态
def add_user_activity
user_activity = UserActivity.where("act_type = '#{self.contest_act_type.to_s}' and act_id = '#{self.contest_act_id}'").first
if user_activity
user_activity.save
else
if self.contest_act_type == 'Message' && !self.contest_act.parent_id.nil?
user_activity = UserActivity.where("act_type = 'Message' and act_id = #{self.contest_act.parent.id}").first
user_activity.created_at = self.created_at
user_activity.save
else
user_activity = UserActivity.new
user_activity.act_id = self.contest_act_id
user_activity.act_type = self.contest_act_type
user_activity.container_type = "Contest"
user_activity.container_id = self.contest_id
user_activity.user_id = self.user_id
user_activity.save
end
end
end
def destroy_user_activity
user_activity = UserActivity.where("act_type = '#{self.contest_act_type.to_s}' and act_id = '#{self.contest_act_id}'")
user_activity.destroy_all
end
end