I have a STI model where the subclasses of each use different validations. The Rails defaults will run the original type's validations at runtime, so I am trying to use "becomes" to force the validations of the new type.
My code looks like this:
payment_processor = PaymentProcessor.where(:account_id => self.id).first_or_initialize
new_gateway = "PaymentProcessors::#{gateway_type.classify}".constantize
payment_processor = payment_processor.becomes(new_gateway)
payment_processor.type = new_gateway
payment_processor.assign_attributes(attributes)
payment_processor.save!
However, it won't save because the MySQl generated during save is looking for the new type. So, for example, if my initial gateway_type is "AuthorizeNet" and I'm changing to "PayPal", the MySQL is:
UPDATE `payment_processors` SET `type` = 'PaymentProcessors::PayPal', `updated_at` = '2015-11-07 11:53:53' WHERE `payment_processors`.`type` IN ('PaymentProcessors::PayPal') AND `payment_processors`.`id` = 232
But it should be looking for the original type, Auth.net like this:
UPDATE `payment_processors` SET `type` = 'PaymentProcessors::PayPal', `updated_at` = '2015-11-07 11:53:53' WHERE `payment_processors`.`type` IN ('PaymentProcessors::AuthorizeNet') AND `payment_processors`.`id` = 232
Any ideas on how to skip the "where clause" to just update by the payment_processor ID?