class CourseMessage < ActiveRecord::Base attr_accessible :course_id, :course_message_id, :course_message_type, :user_id, :viewed # 多态 虚拟关联 belongs_to :course_message ,:polymorphic => true belongs_to :course belongs_to :user validates :user_id,presence: true validates :course_id,presence: true validates :course_message_id,presence: true validates :course_message_type, presence: true end