个人动态的级联新增和级联删除

This commit is contained in:
sw 2015-08-21 14:21:10 +08:00
parent b52f607b68
commit 2db6696fd4
2 changed files with 45 additions and 0 deletions

View File

@ -4,4 +4,27 @@ class CourseActivity < ActiveRecord::Base
belongs_to :course_act ,:polymorphic => true
belongs_to :course
belongs_to :user
has_many :user_acts, :class_name => 'UserAcivity',:as =>:act
after_save :add_user_activity
before_destroy :destroy_user_activity
#在个人动态里面增加当前动态
def add_user_activity
user_activity = UserActivity.where("act_type = '#{self.class.to_s}' and act_id = '#{self.id}'").first
if user_activity
user_activity.save
else
user_activity = UserActivity.new
user_activity.act_id = self.id
user_activity.act_type = self.class.to_s
user_activity.container_type = "Course"
user_activity.container_id = self.course_id
user_activity.save
end
end
def destroy_user_activity
user_activity = UserActivity.where("act_type = '#{self.class.to_s}' and act_id = '#{self.id}'")
user_activity.destroy_all
end
end

View File

@ -19,5 +19,27 @@ class ForgeActivity < ActiveRecord::Base
validates :project_id,presence: true
validates :forge_act_id,presence: true
validates :forge_act_type, presence: true
has_many :user_acts, :class_name => 'UserAcivity',:as =>:act
after_save :add_user_activity
before_destroy :destroy_user_activity
#在个人动态里面增加当前动态
def add_user_activity
user_activity = UserActivity.where("act_type = '#{self.class.to_s}' and act_id = '#{self.id}'").first
if user_activity
user_activity.save
else
user_activity = UserActivity.new
user_activity.act_id = self.id
user_activity.act_type = self.class.to_s
user_activity.container_type = "Project"
user_activity.container_id = self.project_id
user_activity.save
end
end
def destroy_user_activity
user_activity = UserActivity.where("act_type = '#{self.class.to_s}' and act_id = '#{self.id}'")
user_activity.destroy_all
end
end