socialforge/app/models/course_group.rb

32 lines
1.0 KiB
Ruby

class CourseGroup < ActiveRecord::Base
# attr_accessible :title, :body
belongs_to :course
has_many :members
has_many :memberships, :class_name => 'Member'
has_many :member_principals, :class_name => 'Member',
:include => :principal,
:conditions => "#{Principal.table_name}.type='Group' OR (#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{Principal::STATUS_ACTIVE})"
has_many :principals, :through => :member_principals, :source => :principal
has_many :users, :through => :members
before_destroy :set_member_nil
attr_accessible :name
validates :name, :presence => true, :length => {:maximum => 20}
validate_on_create :unique_name_and_course
def set_member_nil
if self.members && self.members.count > 0
self.members.update_all("course_group_id = 0")
end
end
private
def unique_name_and_course
if CourseGroup.find(:conditions => ["name=? and course_id=?", name, course_id]).first
errors.add_to_base("该班名已被使用!")
end
end
end