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}, :uniqueness => { :scope => :course_id}



  def set_member_nil
    if self.members && self.members.count > 0
       self.members.update_all("course_group_id = 0")
    end
  end

end
