Signed-off-by: alan <547533434@qq.com>

This commit is contained in:
alan 2015-01-23 10:03:00 +08:00
parent 8f1249390a
commit 24a8f4fe7d
2 changed files with 12 additions and 2 deletions

View File

@ -273,6 +273,7 @@ class CoursesController < ApplicationController
if valid_attr.eql?('name') if valid_attr.eql?('name')
faker.name = valid_value faker.name = valid_value
# faker.course_id = @course.id
faker.valid? faker.valid?
req[:valid] = faker.errors[:name].blank? req[:valid] = faker.errors[:name].blank?
req[:message] = faker.errors[:name] req[:message] = faker.errors[:name]

View File

@ -12,11 +12,20 @@ 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 => {case_sensitive: false} validate_on_create :unique_name_and_course
def set_member_nil def set_member_nil
if self.members && self.members.count > 0 if self.members && self.members.count > 0
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.find(:conditions => ["name=? and course_id=?", name, course_id]).first
errors.add_to_base("该班名已被使用!")
end
end
end end