# Converting class or type under single table inheritance

**URL:** <https://www.ruby-forum.com/t/converting-class-or-type-under-single-table-inheritance/61240>\
**Category:** Ruby\
**Created:** [May 22, 2006, 10:48am UTC](https://www.ruby-forum.com/t/converting-class-or-type-under-single-table-inheritance/61240 "2006-05-22T10:48:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![levent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@levent](https://www.ruby-forum.com/u/levent)\
**Post date:** [May 22, 2006, 10:48am UTC](https://www.ruby-forum.com/t/converting-class-or-type-under-single-table-inheritance/61240/1 "2006-05-22T10:48:13Z")

</div>

I think this query is more Ruby than Rails but I’m writing a Rails app  
using single table inheritance

class User \< ActiveRecord::Base  
end

class AdminUser \< User  
end

The type column in the users table holds the class name for each  
instance

Now I want to convert a User object into an AdminUser. Is this  
possible? Does anyone know the best way to do this?

Thanks in advance  
levent

---

<div class="post-metadata">

**Author:** ![levent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@levent](https://www.ruby-forum.com/u/levent)\
**Post date:** [May 22, 2006, 11:21am UTC](https://www.ruby-forum.com/t/converting-class-or-type-under-single-table-inheritance/61240/2 "2006-05-22T11:21:57Z")

</div>

user[:type] = AdminUser

---

<div class="post-metadata">

**Author:** ![levent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@levent](https://www.ruby-forum.com/u/levent)\
**Post date:** [May 22, 2006, 11:43am UTC](https://www.ruby-forum.com/t/converting-class-or-type-under-single-table-inheritance/61240/3 "2006-05-22T11:43:24Z")

</div>

Joey wrote:

> user[:type] = AdminUser

that doesn’t seem to work… that throws “TypeError: can’t dump anonymous  
class Class”

I’ve already tried stuff like:  
@user = User.find(1) # an object of type User not AdminUser  
@user.type = AdminUser  
@user.save # Throws the same error as above

@user[:type] = AdminUser  
@user.save # Throws the same error

---

<div class="post-metadata">

**Author:** ![levent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@levent](https://www.ruby-forum.com/u/levent)\
**Post date:** [May 22, 2006, 1:51pm UTC](https://www.ruby-forum.com/t/converting-class-or-type-under-single-table-inheritance/61240/4 "2006-05-22T13:51:45Z")

</div>

On Mon, May 22, 2006 at 06:43:31PM +0900, Levent A. wrote:  
} Joey wrote:  
} \> user[:type] = AdminUser  
}  
} that doesn’t seem to work… that throws “TypeError: can’t dump  
anonymous  
} class Class”  
}  
} I’ve already tried stuff like:  
} @user = User.find(1) # an object of type User not AdminUser  
} @user.type = AdminUser  
} @user.save # Throws the same error as above  
}  
} @user[:type] = AdminUser  
} @user.save # Throws the same error

You can’t set it to the class, you need to set it to the name. It’s a  
subtlety:

user[:type] = ‘AdminUser’

–Greg

---

<div class="post-metadata">

**Author:** ![levent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@levent](https://www.ruby-forum.com/u/levent)\
**Post date:** [May 22, 2006, 1:58pm UTC](https://www.ruby-forum.com/t/converting-class-or-type-under-single-table-inheritance/61240/5 "2006-05-22T13:58:04Z")

</div>

Gregory S. wrote:

> On Mon, May 22, 2006 at 06:43:31PM +0900, Levent A. wrote:  
> } Joey wrote:  
> } \> user[:type] = AdminUser  
> }  
> } that doesn’t seem to work… that throws “TypeError: can’t dump  
> anonymous  
> } class Class”  
> }  
> } I’ve already tried stuff like:  
> } @user = User.find(1) # an object of type User not AdminUser  
> } @user.type = AdminUser  
> } @user.save # Throws the same error as above  
> }  
> } @user[:type] = AdminUser  
> } @user.save # Throws the same error
> 
> You can’t set it to the class, you need to set it to the name. It’s a  
> subtlety:
> 
> user[:type] = ‘AdminUser’
> 
> –Greg

I’m an idiot 🙂

Thanks… I can’t believe I didn’t think of that… that’s what happens  
when you spend hours on a simple thing, over thinking the problem…  
thanks again

levent
