2014-12-02 14:41:36 +08:00
class CourseGroup < ActiveRecord :: Base
# attr_accessible :title, :body
belongs_to :course
2014-12-04 22:02:10 +08:00
has_many :members
2014-12-04 19:09:10 +08:00
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
2014-12-02 14:41:36 +08:00
before_destroy :set_member_nil
attr_accessible :name
2015-01-23 10:03:00 +08:00
validates :name , :presence = > true , :length = > { :maximum = > 20 }
2015-01-23 11:46:55 +08:00
validate :unique_name_and_course
2015-01-23 10:03:00 +08:00
2014-12-02 14:41:36 +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
2015-01-23 11:46:55 +08:00
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
2014-12-02 14:41:36 +08:00
end