2012-03-05 43 views
0

我对名为'user_name'且范围为'customer_id'和'active'的列使用client_side_validation唯一性。我只是想抛出一个错误,如果用户名已经存在于表中,只有当客户ID是存在的和活动列是真实的,并允许用户名称,如果活动列是假的请有人帮助我。rails客户端验证mysql数据库中布尔值的唯一性

回答

0

也许这将帮助:

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 
+0

感谢您的答复先生,但我使用客户端验证的宝石不执行任何功能,只需要在验证器类,如存在,唯一性的特定属性。我怀疑,在实时验证过程中,mysql数据库布尔值“0”或“1”是否正确检查为“true”或“false” – nishanthan 2012-03-06 07:56:10