How to cast an ActiveRecord object to another class when using STI?

Viewed 21288

I'm currently using ActiveRecord single table inheritance.

How can I cast one of my models from type A to B? They have the same parent.

4 Answers

If we have something like following

class A < ApplicationRecord
end

Class B < A
end

we can use becomes

a = A.first
b = a.becomes(B)

or vice versa

Related