# Validations of "type" when using Single Table Inheritance

**URL:** <https://www.ruby-forum.com/t/validations-of-type-when-using-single-table-inheritance/108709>\
**Category:** Rails\
**Created:** [July 6, 2007, 6:38am UTC](https://www.ruby-forum.com/t/validations-of-type-when-using-single-table-inheritance/108709 "2007-07-06T06:38:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jaime\_Cham](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@Jaime\_Cham](https://www.ruby-forum.com/u/Jaime_Cham)\
**Post date:** [July 6, 2007, 6:38am UTC](https://www.ruby-forum.com/t/validations-of-type-when-using-single-table-inheritance/108709/1 "2007-07-06T06:38:05Z")

</div>

Sorry if I’m just dumbing out, but is there a reason that when using  
Single Table Inheritance Rails doesn’t really validate the “type”  
field or provide some mechanism to do so? seems like a good way to  
corrupt a database. Because the “type” is also a special attribute, I  
had to resort to an ugly hack like:

class ActiveRecord::Base  
def type\_attr  
self[:type]  
end  
def self.validates\_type(options={})  
validates\_inclusion\_of :type\_attr, options  
end  
end

so that I could write:

class Fruit \< ActiveRecord::Base

```
validates_type :allow_nil => true,
                        :in => %w{ Apple } # could be dynamic

```

subclass\_of but make faster!

(obviously this could also have been put on the derived class)

Thanks!

---

<div class="post-metadata">

**Author:** ![Jaime\_Cham](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@Jaime\_Cham](https://www.ruby-forum.com/u/Jaime_Cham)\
**Post date:** [July 9, 2007, 12:07am UTC](https://www.ruby-forum.com/t/validations-of-type-when-using-single-table-inheritance/108709/2 "2007-07-09T00:07:49Z")

</div>

I’d dare to say the reason “type” can’t be validated is that it  
inherently has to exist.

After all, the “type” is exactly the name of the class on which you’re  
calling “create”

if the “type” doesn’t exist,  
then you can’t be trying to save it.

Jaime Cham wrote:

> Sorry if I’m just dumbing out, but is there a reason that when using  
> Single Table Inheritance Rails doesn’t really validate the “type”  
> field or provide some mechanism to do so? seems like a good way to  
> corrupt a database. Because the “type” is also a special attribute, I  
> had to resort to an ugly hack like:
> 
> class ActiveRecord::Base  
> def type\_attr  
> self[:type]  
> end  
> def self.validates\_type(options={})  
> validates\_inclusion\_of :type\_attr, options  
> end  
> end
> 
> so that I could write:
> 
> class Fruit \< ActiveRecord::Base
> 
> ```
> validates_type :allow_nil => true,
> :in => %w{ Apple } # could be dynamic
> 
> ```
> 
> subclass\_of but make faster!
> 
> (obviously this could also have been put on the derived class)
> 
> Thanks!

---

<div class="post-metadata">

**Author:** ![Jaime\_Cham](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/258eb7/32.png) [@Jaime\_Cham](https://www.ruby-forum.com/u/Jaime_Cham)\
**Post date:** [July 10, 2007, 10:03am UTC](https://www.ruby-forum.com/t/validations-of-type-when-using-single-table-inheritance/108709/3 "2007-07-10T10:03:53Z")

</div>

Yes, although it could be changed manually (unlikely) or injected from  
cgi parameters, which is what the validations are supposed to prevent,  
no?

On Jul 9, 6:07 am, Matthew R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
