# Single table inheritance: change object 'type' after create?

**URL:** <https://www.ruby-forum.com/t/single-table-inheritance-change-object-type-after-create/52489>\
**Category:** Rails\
**Created:** [January 15, 2006, 1:53am UTC](https://www.ruby-forum.com/t/single-table-inheritance-change-object-type-after-create/52489 "2006-01-15T01:53:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![asa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dfb087/32.png) [@asa](https://www.ruby-forum.com/u/asa)\
**Post date:** [January 15, 2006, 1:53am UTC](https://www.ruby-forum.com/t/single-table-inheritance-change-object-type-after-create/52489/1 "2006-01-15T01:53:08Z")

</div>

Hey folks,

I’m trying to keep track of couple of different kinds of “entities” in  
my rails app: “people”, “organizations”, and “committees”. I have a  
single Entities table in the database, and I’m using Single Table  
Inheritance to keep the different subclasses in the same table, with a  
field “type”.

My problem comes when allowing the user to correct a mistake – say,  
change a “committee” to a more general “organization”.  
@entity.update\_attributes({:type=\>“Organization”}) doesn’t change the  
database. I can understand that ruby doesn’t want me changing the class  
of an existing object. However, is there a way I can make this change,  
while keeping the entity’s same “id” (so I don’t have to change  
everywhere that’s referenced)?

Thanks a bunch for your help,  
Asa

---

<div class="post-metadata">

**Author:** ![asa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dfb087/32.png) [@asa](https://www.ruby-forum.com/u/asa)\
**Post date:** [January 15, 2006, 4:01am UTC](https://www.ruby-forum.com/t/single-table-inheritance-change-object-type-after-create/52489/2 "2006-01-15T04:01:46Z")

</div>

Asa H. wrote:

> of an existing object. However, is there a way I can make this change,  
> while keeping the entity’s same “id” (so I don’t have to change  
> everywhere that’s referenced)?

The STI “type” attribute is protected from mass assignment, so instead  
use:

```
@entity.update_attribute( :type, 'Organization' )

```

or

```
@entity[:type] = ...
...
@entity.save

```

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![asa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dfb087/32.png) [@asa](https://www.ruby-forum.com/u/asa)\
**Post date:** [January 15, 2006, 5:19am UTC](https://www.ruby-forum.com/t/single-table-inheritance-change-object-type-after-create/52489/3 "2006-01-15T05:19:45Z")

</div>

Thanks! That worked right away.

Asa

Mark Reginald J. wrote:

> The STI “type” attribute is protected from mass assignment, so instead  
> use:
> 
> ```
> @entity.update_attribute( :type, 'Organization' )
> 
> ```
> 
> or
> 
> ```
> @entity[:type] = ...
> ...
> @entity.save
> 
> ```
> 
> –  
> We develop, watch us RoR, in numbers too big to ignore.
