2015-08-11 15:54:05 +08:00
|
|
|
class CourseActivity < ActiveRecord::Base
|
|
|
|
attr_accessible :user_id, :course_act_id,:course_act_type,:course_id
|
|
|
|
# 虚拟关联
|
2015-08-11 17:02:52 +08:00
|
|
|
belongs_to :course_act ,:polymorphic => true
|
2015-08-11 15:54:05 +08:00
|
|
|
belongs_to :course
|
|
|
|
belongs_to :user
|
2015-08-21 14:21:10 +08:00
|
|
|
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
|
2015-08-25 17:54:29 +08:00
|
|
|
user_activity.act_id = self.course_act_id
|
|
|
|
user_activity.act_type = self.course_act_type
|
2015-08-21 14:21:10 +08:00
|
|
|
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
|
2015-08-11 15:54:05 +08:00
|
|
|
end
|