自定义校验:当创建服务时,该项目下已存在该服务名称,抛出该服务已经存在的错误。
validate :validate_service_unique, on: :create
private
def validate_service_unique
if Service.where(:project_id => self.project_id, :name => self.name).length > 0
self.errors.add(:name, "name has already been taken")
end
end