STI and ActiveRecord attributes unprotected


#1

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?


#2

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 :wink:

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


#3

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 :wink:


#4

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 :wink:

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.