Converting STI objects

Hi,

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
object.
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!
Jean-Etienne

One way is to manually set the type column:

obj[:type] = Class2
obj.save

Chris

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
save…

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