2015-08-14 10:19:59 +08:00
|
|
|
class CourseMessage < ActiveRecord::Base
|
2015-08-26 14:47:53 +08:00
|
|
|
attr_accessible :course_id, :course_message_id, :course_message_type, :user_id, :viewed, :content, :status
|
2015-08-14 10:19:59 +08:00
|
|
|
|
|
|
|
# 多态 虚拟关联
|
|
|
|
belongs_to :course_message ,:polymorphic => true
|
|
|
|
belongs_to :course
|
|
|
|
belongs_to :user
|
2015-08-24 22:47:48 +08:00
|
|
|
has_many :message_alls, :class_name => 'MessageAll',:as =>:message, :dependent => :destroy
|
|
|
|
|
2015-08-14 10:19:59 +08:00
|
|
|
validates :user_id,presence: true
|
|
|
|
validates :course_id,presence: true
|
|
|
|
validates :course_message_id,presence: true
|
|
|
|
validates :course_message_type, presence: true
|
2015-08-29 17:09:44 +08:00
|
|
|
validates_length_of :content, :maximum => 100
|
2015-08-24 22:47:48 +08:00
|
|
|
after_create :add_user_message
|
|
|
|
|
|
|
|
def add_user_message
|
|
|
|
self.message_alls << MessageAll.new(:user_id => self.user_id)
|
|
|
|
end
|
2015-08-14 10:19:59 +08:00
|
|
|
end
|