【问题标题】:rails client side validation uniqueness for boolean value in mysql databasemysql数据库中布尔值的rails客户端验证唯一性
【发布时间】:2012-03-05 15:35:11
【问题描述】:

我正在对名为“user_name”的列使用 client_side_validation 唯一性,其范围为“customer_id”和“active”。如果用户名已经存在于表中,我只想抛出一个错误,只有当客户 ID 存在并且活动列为真时,如果活动列为假,则允许用户名请有人帮助我。

【问题讨论】:

    标签: mysql ruby-on-rails-3 boolean client-side-validation


    【解决方案1】:

    也许这会有所帮助:

    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
    

    【讨论】:

    • 感谢您的回复先生,但我使用的是客户端验证 gem,它不执行任何功能,它只需要验证器类,例如存在,特定属性的唯一性。我怀疑,在实时验证过程中是否正确检查了 mysql 数据库布尔值“0”或“1”是否为“真”或“假”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    相关资源
    最近更新 更多