Switching an ActiveRecord instance between STI classes

Sometimes you wish to convert one STI class to another, e.g. promote User to Administrator.

ActiveRecord::Base#becomes(Klass) returns an instance of the current record converted to the specified Klass, e.g.:

 
user = User.find_by_login("sam")
user.becomes(Administrator)

Note however that this does not change the type attribute of the instance, so you still have an additional step to do if you want this conversion to be permanent.

You must be logged in to post a comment.