# Odd behaviour of update\_attributes in STI table

**URL:** <https://www.ruby-forum.com/t/odd-behaviour-of-update-attributes-in-sti-table/177505>\
**Category:** Rails\
**Created:** [October 19, 2009, 11:08am UTC](https://www.ruby-forum.com/t/odd-behaviour-of-update-attributes-in-sti-table/177505 "2009-10-19T11:08:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 19, 2009, 11:08am UTC](https://www.ruby-forum.com/t/odd-behaviour-of-update-attributes-in-sti-table/177505/1 "2009-10-19T11:08:13Z")

</div>

I just noticed this odd behaviour (in rails 2.2.2). It looks like  
changing the type with update\_attributes _doesn’t_ work (but returns  
true!), but changing the field and then saving it _does_ work. Here i  
have an STI situation where TeachingObject and LearningObject extend  
Resource.

The bit where update\_attributes returns true, yet seems to fail, is  
rather disconcerting. Can anyone explain what’s going on here?

thanks -max

> > resource = Resource.first  
> > =\> #\<TeachingObject id: 59, name: “Clarinet bite point close-up”,  
> > description: “”, type: “TeachingObject”, \>
> 
> > resource.update\_attributes(:type =\> “LearningObject”)  
> > =\> true
> 
> > resource = Resource.first  
> > =\> #\<TeachingObject id: 59, name: “Clarinet bite point close-up”,  
> > description: “”, type: “TeachingObject”, \>
> 
> > resource.type = “LearningObject”  
> > =\> “LearningObject”
> 
> > resource.save  
> > =\> true
> 
> > resource = Resource.first  
> > =\> #\<LearningObject id: 59, name: “Clarinet bite point close-up”,  
> > description: “”, type: “LearningObject”, \>

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 19, 2009, 1:07pm UTC](https://www.ruby-forum.com/t/odd-behaviour-of-update-attributes-in-sti-table/177505/2 "2009-10-19T13:07:47Z")

</div>

On Oct 19, 10:08 am, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I just noticed this odd behaviour (in rails 2.2.2). It looks like  
> changing the type with update\_attributes _doesn’t_ work (but returns  
> true!), but changing the field and then saving it _does_ work. Here i  
> have an STI situation where TeachingObject and LearningObject extend  
> Resource.
> 
> The bit where update\_attributes returns true, yet seems to fail, is  
> rather disconcerting. Can anyone explain what’s going on here?

I believe that the type column is marked as a protected attribute or  
something along those lines.

Fred

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 19, 2009, 1:23pm UTC](https://www.ruby-forum.com/t/odd-behaviour-of-update-attributes-in-sti-table/177505/3 "2009-10-19T13:23:16Z")

</div>

Frederick C. wrote:

> On Oct 19, 10:08ï¿½am, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > I just noticed this odd behaviour (in rails 2.2.2). ï¿½ It looks like  
> > changing the type with update\_attributes _doesn’t_ work (but returns  
> > true!), but changing the field and then saving it _does_ work. ï¿½Here i  
> > have an STI situation where TeachingObject and LearningObject extend  
> > Resource.
> > 
> > The bit where update\_attributes returns true, yet seems to fail, is  
> > rather disconcerting. ï¿½Can anyone explain what’s going on here?
> 
> I believe that the type column is marked as a protected attribute or  
> something along those lines.
> 
> Fred

ahhh, well that would explain it. It would also explain why  
update\_attribute returns true - it just silently ignores the protected  
fields. It’s a bit annoyin that the api doesn’t mention this: to quote

> > Single table inheritance
> > 
> > Active Record allows inheritance by storing the name of the class in a column that by default is named “type” (can be changed by overwriting Base.inheritance\_column). This means that an inheritance looking like this:
> > 
> > class Company \< ActiveRecord::Base; end  
> > class Firm \< Company; end  
> > class Client \< Company; end  
> > class PriorityClient \< Client; end
> > 
> > When you do Firm.create(:name =\> “37signals”), this record will be saved in the companies table with type = “Firm”. You can then fetch this row again using Company.find(:first, “name = â€˜37signalsâ€™”) and it will return a Firm object.
> > 
> > If you donâ€˜t have a type column defined in your table, single-table inheritance wonâ€˜t be triggered. In that case, itâ€˜ll work just like normal subclasses with no special magic for differentiating between them or reloading the right type with find.
> > 
> > Note, all the attributes for all the cases are kept in the same table. Read more: [P of EAA: Single Table Inheritance](http://www.martinfowler.com/eaaCatalog/singleTableInheritance.html)

unquote.

anyway, thanks Fred.  
max
