socialforge/app/models/course_group.rb

32 lines
1013 B
Ruby
Raw Normal View History

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
2015-01-23 10:03:00 +08:00
validates :name, :presence => true, :length => {:maximum => 20}
validate :unique_name_and_course
2015-01-23 10:03:00 +08:00
def set_member_nil
if self.members && self.members.count > 0
self.members.update_all("course_group_id = 0")
end
end
2015-01-23 10:03:00 +08:00
private
def unique_name_and_course
if CourseGroup.where("name=? and course_id=?", name, course_id).first
errors.add(:name, :groupname_repeat)
2015-01-23 10:03:00 +08:00
end
end
end