From 24a8f4fe7d7c5c0b9e4798a5baf255bbbc217e12 Mon Sep 17 00:00:00 2001 From: alan <547533434@qq.com> Date: Fri, 23 Jan 2015 10:03:00 +0800 Subject: [PATCH] Signed-off-by: alan <547533434@qq.com> --- app/controllers/courses_controller.rb | 1 + app/models/course_group.rb | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index edd3f1b9d..dcd09959d 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -273,6 +273,7 @@ class CoursesController < ApplicationController if valid_attr.eql?('name') faker.name = valid_value + # faker.course_id = @course.id faker.valid? req[:valid] = faker.errors[:name].blank? req[:message] = faker.errors[:name] diff --git a/app/models/course_group.rb b/app/models/course_group.rb index 144c43c6e..c1236da93 100644 --- a/app/models/course_group.rb +++ b/app/models/course_group.rb @@ -12,11 +12,20 @@ class CourseGroup < ActiveRecord::Base before_destroy :set_member_nil attr_accessible :name - validates :name, :presence => true, :length => {:maximum => 20}, - :uniqueness => {case_sensitive: false} + validates :name, :presence => true, :length => {:maximum => 20} + validate_on_create :unique_name_and_course + + + def set_member_nil if self.members && self.members.count > 0 self.members.update_all("course_group_id = 0") 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