我正在为作用域为"customer_id"one_answers"active"的名为"user_name"的列使用client_side_validation唯一性。如果用户名已经存在于表中,我只想抛出一个错误,仅当客户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