【发布时间】:2012-03-05 15:35:11
【问题描述】:
我正在对名为“user_name”的列使用 client_side_validation 唯一性,其范围为“customer_id”和“active”。如果用户名已经存在于表中,我只想抛出一个错误,只有当客户 ID 存在并且活动列为真时,如果活动列为假,则允许用户名请有人帮助我。
【问题讨论】:
标签: mysql ruby-on-rails-3 boolean client-side-validation
我正在对名为“user_name”的列使用 client_side_validation 唯一性,其范围为“customer_id”和“active”。如果用户名已经存在于表中,我只想抛出一个错误,只有当客户 ID 存在并且活动列为真时,如果活动列为假,则允许用户名请有人帮助我。
【问题讨论】:
标签: mysql ruby-on-rails-3 boolean client-side-validation
也许这会有所帮助:
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
【讨论】: