ฉันจะตรวจสอบการมีอยู่ของฟิลด์หนึ่งหรืออีกฟิลด์หนึ่งได้อย่างไร แต่ไม่ใช่ทั้งสองฟิลด์และอย่างน้อยหนึ่งฟิลด์
ฉันจะตรวจสอบการมีอยู่ของฟิลด์หนึ่งหรืออีกฟิลด์หนึ่งได้อย่างไร แต่ไม่ใช่ทั้งสองฟิลด์และอย่างน้อยหนึ่งฟิลด์
คำตอบ:
รหัสของคุณจะทำงานหากคุณเพิ่มเงื่อนไขในการตรวจสอบความถูกต้องของตัวเลขดังนี้:
class Transaction < ActiveRecord::Base
    validates_presence_of :date
    validates_presence_of :name
    validates_numericality_of :charge, allow_nil: true
    validates_numericality_of :payment, allow_nil: true
    validate :charge_xor_payment
  private
    def charge_xor_payment
      unless charge.blank? ^ payment.blank?
        errors.add(:base, "Specify a charge or a payment, not both")
      end
    end
end
ฉันคิดว่านี่เป็นสำนวนมากกว่าใน Rails 3+:
เช่น: สำหรับการตรวจสอบว่าหนึ่งuser_nameหรือemailเป็นปัจจุบัน:
validates :user_name, presence: true, unless: ->(user){user.email.present?}
validates :email, presence: true, unless: ->(user){user.user_name.present?}
class Transaction < ActiveRecord::Base
    validates_presence_of :date
    validates_presence_of :name
    validates_numericality_of :charge, allow_nil: true
    validates_numericality_of :payment, allow_nil: true
    validate :charge_xor_payment
  private
    def charge_xor_payment
      if [charge, payment].compact.count != 1
        errors.add(:base, "Specify a charge or a payment, not both")
      end
    end
end
คุณสามารถทำได้ด้วย 3 ค่าขึ้นไป:
if [month_day, week_day, hour].compact.count != 1
ตัวอย่างราง 3.
class Transaction < ActiveRecord::Base
  validates_presence_of :date
  validates_presence_of :name
  validates_numericality_of :charge, :unless => proc{|obj| obj.charge.blank?}
  validates_numericality_of :payment, :unless => proc{|obj| obj.payment.blank?}
  validate :charge_xor_payment
  private
    def charge_xor_payment
      if !(charge.blank? ^ payment.blank?)
        errors[:base] << "Specify a charge or a payment, not both"
      end
    end
end
 validate :father_or_mother
# นามสกุลพ่อหรือนามสกุลแม่เป็นเรื่องบังคับ
 def father_or_mother
        if father_last_name == "Last Name" or father_last_name.blank?
           errors.add(:father_last_name, "cant blank")
           errors.add(:mother_last_name, "cant blank")
        end
 end
ลองใช้ตัวอย่างง่ายๆข้างต้น
ฉันตอบคำถามนี้ไว้ด้านล่าง ในตัวอย่างนี้:descriptionและ:keywordsเป็นฟิลด์ที่ฟิลด์นี้ไม่เว้นว่าง:
  validate :some_was_present
  belongs_to :seo_customable, polymorphic: true
  def some_was_present
    desc = description.blank?
    errors.add(desc ? :description : :keywords, t('errors.messages.blank')) if desc && keywords.blank?
  end
การตรวจสอบความถูกต้องโดยใช้Proc หรือ Symbol ด้วย: if และ: เว้นแต่จะได้รับการเรียกใช้อย่างถูกต้องก่อนการตรวจสอบจะเกิดขึ้น
ดังนั้นการมีหนึ่งในทั้งสองฟิลด์อาจเป็นดังนี้:
validates :charge,
  presence: true,
  if: ->(user){user.charge.present? || user.payment.present?}
validates :payment,
  presence: true,
  if: ->(user){user.payment.present? || user.charge.present?}
โค้ด (ตัวอย่างข้อมูลโค้ด) มี:ifหรือ:unlessเป็นรายการล่าสุดอย่างไรก็ตามตามที่ประกาศไว้ในเอกสารจะถูกเรียกใช้ก่อนที่การตรวจสอบจะเกิดขึ้นดังนั้นการตรวจสอบอีกครั้งจะทำงานหลังจากนั้นหากตรงตามเงื่อนไข