# How to change class of an STI object?

**URL:** <https://www.ruby-forum.com/t/how-to-change-class-of-an-sti-object/90277>\
**Category:** Rails\
**Created:** [March 27, 2007, 10:51am UTC](https://www.ruby-forum.com/t/how-to-change-class-of-an-sti-object/90277 "2007-03-27T10:51:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbebop](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cdc98d/32.png) [@bbebop](https://www.ruby-forum.com/u/bbebop)\
**Post date:** [March 27, 2007, 10:51am UTC](https://www.ruby-forum.com/t/how-to-change-class-of-an-sti-object/90277/1 "2007-03-27T10:51:46Z")

</div>

I have some models which use the same table via single table  
inheritance. I (think I) need to change objects from one class to  
another and not sure how to go about that.

Consider these models:

class Project \< ActiveRecord::Base  
end

class Goal \< Project # inherits project characteristics  
end

class Assignment \< Project # inherits project characteristics  
end

They are distinguished from each other by the ‘type’ column.

class CreateProjects \< ActiveRecord::Migration  
def self.up  
create\_table :projects do |t|  
t.column :type, :string  
…  
end  
end  
end

How would I convert a ‘Project’ object into a ‘Goal’ object while  
preserving other characteristics (id, title, etc.)?

Thanks!

---

<div class="post-metadata">

**Author:** ![bbebop](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cdc98d/32.png) [@bbebop](https://www.ruby-forum.com/u/bbebop)\
**Post date:** [March 27, 2007, 5:53pm UTC](https://www.ruby-forum.com/t/how-to-change-class-of-an-sti-object/90277/2 "2007-03-27T17:53:06Z")

</div>

Just change the value in the type column to change the type to any of  
the other types. I define a method that aliases to type to make it  
easier to access. You will need to reload the data to get a different  
Ruby object.

Michael

---

<div class="post-metadata">

**Author:** ![bbebop](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/cdc98d/32.png) [@bbebop](https://www.ruby-forum.com/u/bbebop)\
**Post date:** [March 27, 2007, 7:24pm UTC](https://www.ruby-forum.com/t/how-to-change-class-of-an-sti-object/90277/3 "2007-03-27T19:24:23Z")

</div>

Thanks Michael. That’s simple enough!
