# STI updating the :type attribute of an STI instance

**URL:** <https://www.ruby-forum.com/t/sti-updating-the-type-attribute-of-an-sti-instance/153551>\
**Category:** Rails\
**Created:** [November 18, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/sti-updating-the-type-attribute-of-an-sti-instance/153551 "2008-11-18T17:35:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [November 18, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/sti-updating-the-type-attribute-of-an-sti-instance/153551/1 "2008-11-18T17:35:50Z")

</div>

Hi !

i’ve different users subclasses organised thanks to STI under a user  
mother class, problem is that i can’t find a way to exchange a user form  
a certain type to an other , any light ???

class User \< ActiveRecord::Base

end

class Guest \< User  
has\_and\_belongs\_to\_many :hosts,  
:join\_table =\> ‘invitations’  
end

class Host \< User  
has\_and\_belongs\_to\_many :guests,  
:join\_table =\> ‘invitations’  
end

#i’d like to change a Guest user to make him become Host :

> > g = Guest.first  
> > =\> #\<Guest id: 418519017, type: “Guest”, …#\>  
> > g.update\_attributes!(:type=\>‘Host’)  
> > =\> true  
> > g  
> > =\> #\<Guest id: 418519017, type: “Guest”, …#\>

#It seems that this “type” attribute is protected is there a way to  
by-pass this ?

you can find the pastie of the above code here :  
[http://pastie.org/317885](http://pastie.org/317885)

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [November 18, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/sti-updating-the-type-attribute-of-an-sti-instance/153551/2 "2008-11-18T18:25:10Z")

</div>

Frederick C. wrote:

> On Nov 18, 4:35ï¿½pm, nico Itkin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > =\> #\<Guest id: 418519017, type: “Guest”, …#\>  
> > g.update\_attributes!(:type=\>‘Host’)  
> > =\> true\>\> g
> > 
> > =\> #\<Guest id: 418519017, type: “Guest”, …#\>
> > 
> > #It seems that this “type” attribute is protected is there a way to  
> > by-pass this ?  
> > Yup (and if you check your logs you’d probably see a warning about not  
> > being able to mass assign a protected attribute)
> 
> There’s two things you can play with: Directly setting the type  
> attribute or playing with the becomes method.
> 
> Fred

I had found update\_attribute\_with\_validation\_skipping which updates the  
protected attribute , but becomes is really what i need !! thanks a lot  
!

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [November 18, 2008, 6:06pm UTC](https://www.ruby-forum.com/t/sti-updating-the-type-attribute-of-an-sti-instance/153551/3 "2008-11-18T18:06:20Z")

</div>

On Nov 18, 4:35 pm, nico Itkin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> =\> #\<Guest id: 418519017, type: “Guest”, …#\>  
> g.update\_attributes!(:type=\>‘Host’)  
> =\> true\>\> g
> 
> =\> #\<Guest id: 418519017, type: “Guest”, …#\>
> 
> #It seems that this “type” attribute is protected is there a way to  
> by-pass this ?  
> Yup (and if you check your logs you’d probably see a warning about not  
> being able to mass assign a protected attribute)

There’s two things you can play with: Directly setting the type  
attribute or playing with the becomes method.

Fred

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [November 18, 2008, 6:28pm UTC](https://www.ruby-forum.com/t/sti-updating-the-type-attribute-of-an-sti-instance/153551/4 "2008-11-18T18:28:39Z")

</div>

On Nov 18, 5:25 pm, nico Itkin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Frederick C. wrote:
> 
> I had found update\_attribute\_with\_validation\_skipping which updates the  
> protected attribute , but becomes is really what i need !! thanks a lot  
> !

You don;t need to dig that deep even for updating the attribute - just  
foo.type = ‘Blah’ would do it.  
Mass protection just means that it’s ignored if you do  
update\_attributes, create, new or things like that.

Fred

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [November 18, 2008, 7:09pm UTC](https://www.ruby-forum.com/t/sti-updating-the-type-attribute-of-an-sti-instance/153551/5 "2008-11-18T19:09:04Z")

</div>

Frederick C. wrote:

> On Nov 18, 5:25ï¿½pm, nico Itkin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Frederick C. wrote:
> > 
> > I had found update\_attribute\_with\_validation\_skipping which updates the  
> > protected attribute , but becomes is really what i need !! thanks a lot  
> > !
> 
> You don;t need to dig that deep even for updating the attribute - just  
> foo.type = ‘Blah’ would do it.  
> Mass protection just means that it’s ignored if you do  
> update\_attributes, create, new or things like that.
> 
> Fred

Realize that later, get mad cause update\_attribute was not working

thanks for the explanation

nico

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [November 19, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/sti-updating-the-type-attribute-of-an-sti-instance/153551/6 "2008-11-19T20:16:25Z")

</div>

I guess it is not the right thing to do. You should use something like  
roles and change it, not inheritance.

On Nov 18, 4:09 pm, nico Itkin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
