Rails 7.1: implementing STI and validation callbacks not triggering as expected
I've searched everywhere and can't find a clear answer. I'm currently working on a Rails 7.1 application where I'm using Single Table Inheritance (STI) for a model called `Payment`. I've defined two subclasses: `CreditCardPayment` and `PayPalPayment`. I have a validation callback in the `Payment` model that should ensure that the `amount` is a positive number. However, it seems that the callback is not being triggered when I create an instance of `CreditCardPayment`. Here's the relevant code for my models: ```ruby class Payment < ApplicationRecord validates :amount, numericality: { greater_than: 0 }, if: :amount_present? def amount_present? amount.present? end end class CreditCardPayment < Payment end class PayPalPayment < Payment end ``` When I try to create a new `CreditCardPayment` instance with a negative amount: ```ruby payment = CreditCardPayment.new(amount: -150) if payment.valid? puts 'Payment is valid' else puts payment.errors.full_messages end ``` I am expecting to see validation errors, but instead, the output is: ``` Payment is valid ``` I've checked the database schema and ensured that the `amount` column exists in the `payments` table. I've also tried using `before_validation` callbacks instead of the `validates` statement, but I still don't see any validation errors when the amount is negative. Is there something I'm missing regarding how validations work with STI in this context? Any insights would be greatly appreciated! I'm working on a CLI tool that needs to handle this. Thanks in advance!