Your code will work if you add conditionals to the numericality validations like so:
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
return if charge.blank? ^ payment.blank?
errors.add(:base, 'Specify a charge or a payment, not both')
end
end
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
#Father last name or Mother last name is compulsory
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
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
The (example snippet) code has :if or :unless as latest item, however as declared in doc it will get called right before validation happens - so another checking will works after, if condition match.