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

时间:2012-03-05 15:35:11

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

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

1 个答案:

答案 0 :(得分: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