修改《多字段唯一性判断》

Signed-off-by: alan <547533434@qq.com>
This commit is contained in:
alan 2015-01-30 14:52:36 +08:00
parent 0d6261dfa7
commit a467736a5e
1 changed files with 2 additions and 8 deletions

View File

@ -12,8 +12,7 @@ class CourseGroup < ActiveRecord::Base
before_destroy :set_member_nil before_destroy :set_member_nil
attr_accessible :name attr_accessible :name
validates :name, :presence => true, :length => {:maximum => 20} validates :name, :presence => true, :length => {:maximum => 20}, :uniqueness => { :scope => :course_id}
validate :unique_name_and_course
@ -22,10 +21,5 @@ class CourseGroup < ActiveRecord::Base
self.members.update_all("course_group_id = 0") self.members.update_all("course_group_id = 0")
end end
end end
private
def unique_name_and_course
if CourseGroup.where("name=? and course_id=?", name, course_id).first
errors.add(:name, :groupname_repeat)
end
end
end end