Converting STI objects


Check the following data model:
class BaseClass < ActiveRecord::Base; end
class Class1 < BaseClass; end
class Class2 < BaseClass; end

For some reasons, I would like to “convert” a Class1 object to a Class2
Creating a new Class2 object from Class1 object is not a solution, since
Class1 object is associated to lots of objects and I do not want to
recreate the associations.

obj = BaseClass.find()
=> how to cast obj from Class1 to Class2

Thank you!

One way is to manually set the type column:

obj[:type] = Class2


On May 2, 12:51 pm, Jean-Etienne D. <rails-mailing-l…@andreas-

You might need to use update_attribute to avoid validation checking on

BaseClass.find().update_attribute(:type, Class1)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs