Forum: Ruby on Rails STI and ActiveRecord attributes unprotected

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sergei S. (Guest)
on 2006-03-22 17:56
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?
David Heinemeier H. (Guest)
on 2006-03-22 19:43
(Received via mailing list)
> 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 -- Broadcasting Brain
http://www.basecamphq.com   -- Online project management
http://www.backpackit.com   -- Personal information manager
http://www.rubyonrails.com  -- Web-application framework
Sergei S. (Guest)
on 2006-03-23 04:29
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 ;)
Wilson B. (Guest)
on 2006-03-23 04:43
(Received via mailing list)
On 3/22/06, Sergei S. <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.
This topic is locked and can not be replied to.