# STI and ActiveRecord attributes unprotected

**URL:** <https://www.ruby-forum.com/t/sti-and-activerecord-attributes-unprotected/85355>\
**Category:** Rails\
**Created:** [March 22, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/sti-and-activerecord-attributes-unprotected/85355 "2006-03-22T16:56:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sserdyuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@sserdyuk](https://www.ruby-forum.com/u/sserdyuk)\
**Post date:** [March 22, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/sti-and-activerecord-attributes-unprotected/85355/1 "2006-03-22T16:56:36Z")

</div>

Hi,

This does not seem to be covered anywhere. Since base class extends  
ActiveRecord with a table that has fields for all heirs, would that mean  
that any heir class can access any of those attributes, including ones  
that belong to other heirs?

Or I am missing something?

---

<div class="post-metadata">

**Author:** ![sserdyuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@sserdyuk](https://www.ruby-forum.com/u/sserdyuk)\
**Post date:** [March 22, 2006, 6:43pm UTC](https://www.ruby-forum.com/t/sti-and-activerecord-attributes-unprotected/85355/2 "2006-03-22T18:43:52Z")

</div>

> This does not seem to be covered anywhere. Since base class extends  
> ActiveRecord with a table that has fields for all heirs, would that mean  
> that any heir class can access any of those attributes, including ones  
> that belong to other heirs?
> 
> Or I am missing something?

## You’re correct. All classes in an STI setup has access to all attributes. Usually the answer is: It Just Doesn’t Matter. But if it does to you, you can always use attr\_protected or overwrite the other attributes with accessors that throws an exception. Whatever floats your boat 😉

David Heinemeier H.  
[http://www.loudthinking.com](http://www.loudthinking.com) – Broadcasting Brain  
[http://www.basecamphq.com](http://www.basecamphq.com) – Online project management  
[http://www.backpackit.com](http://www.backpackit.com) – Personal information manager  
[http://www.rubyonrails.com](http://www.rubyonrails.com) – Web-application framework

---

<div class="post-metadata">

**Author:** ![sserdyuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@sserdyuk](https://www.ruby-forum.com/u/sserdyuk)\
**Post date:** [March 23, 2006, 3:29am UTC](https://www.ruby-forum.com/t/sti-and-activerecord-attributes-unprotected/85355/3 "2006-03-23T03:29:07Z")

</div>

David, I have a lot trust in your decisions and opinions, but what about  
things that iterate over attribute collections like scaffolds for  
example? Neither solution will work correctly with them. I am not saying  
this is the end of the world. Just an observation.

> Usually the answer is: It Just Doesn’t Matter. But if it  
> does to you, you can always use attr\_protected or overwrite the other  
> attributes with accessors that throws an exception. Whatever floats  
> your boat 😉

---

<div class="post-metadata">

**Author:** ![sserdyuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@sserdyuk](https://www.ruby-forum.com/u/sserdyuk)\
**Post date:** [March 23, 2006, 3:43am UTC](https://www.ruby-forum.com/t/sti-and-activerecord-attributes-unprotected/85355/4 "2006-03-23T03:43:17Z")

</div>

On 3/22/06, Sergei S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> David, I have a lot trust in your decisions and opinions, but what about  
> things that iterate over attribute collections like scaffolds for  
> example? Neither solution will work correctly with them. I am not saying  
> this is the end of the world. Just an observation.
> 
> > Usually the answer is: It Just Doesn’t Matter. But if it  
> > does to you, you can always use attr\_protected or overwrite the other  
> > attributes with accessors that throws an exception. Whatever floats  
> > your boat 😉

If you can think of a way for ActiveRecord to know which attributes  
belong to which subclass without having to specify them “by hand” in  
the model file, I’d be happy to work on a patch.  
I haven’t been able to come up with anything solid myself; this is  
just a side-effect of the column auto-discovery technique AR uses, in  
my opinion.
