Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql数据库中布尔值的rails客户端验证唯一性_Mysql_Ruby On Rails 3_Boolean_Client Side Validation - Fatal编程技术网

mysql数据库中布尔值的rails客户端验证唯一性

mysql数据库中布尔值的rails客户端验证唯一性,mysql,ruby-on-rails-3,boolean,client-side-validation,Mysql,Ruby On Rails 3,Boolean,Client Side Validation,我正在对作用域为“customer\u id”和“active”的名为“user\u name”的列使用客户端\u验证唯一性。如果用户名已经存在于表中,我只想抛出一个错误,仅当客户id存在且活动列为true时,才允许;如果用户名为false,请有人帮助我。也许这会有帮助: class User < ActiveRecord::Base scope :with_customer_id_and_active, where("customer_id IS NOT NULL AND acti

我正在对作用域为“customer\u id”和“active”的名为“user\u name”的列使用客户端\u验证唯一性。如果用户名已经存在于表中,我只想抛出一个错误,仅当客户id存在且活动列为true时,才允许;如果用户名为false,请有人帮助我。

也许这会有帮助:

class User < ActiveRecord::Base

  scope :with_customer_id_and_active, where("customer_id IS NOT NULL AND active = 1")

  validates :check_uniq_only_with_customer_id_and_active

  def check_uniq_only_with_customer_id_and_active

    if with_customer_id_and_active.where(:user_name => name).count > 0
      errors.add(:user_name, "user name should be uniq for active users")
    end

  end

end
class用户name)。计数>0
错误。添加(:user\u name,“对于活动用户,用户名应为uniq”)
终止
终止
终止

谢谢您的回复,先生,但我使用的是客户端验证gem,它不执行任何功能,它只接受特定属性的validators类,如presence、unique。我怀疑mysql数据库布尔值“0”或“1”是否在实时验证过程中正确检查为“真”或“假”