# ActiveRecord inheritance with types table

**URL:** <https://www.ruby-forum.com/t/activerecord-inheritance-with-types-table/64607>\
**Category:** Rails\
**Created:** [August 15, 2006, 2:25am UTC](https://www.ruby-forum.com/t/activerecord-inheritance-with-types-table/64607 "2006-08-15T02:25:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pielud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/838e76/32.png) [@pielud](https://www.ruby-forum.com/u/pielud)\
**Post date:** [August 15, 2006, 2:25am UTC](https://www.ruby-forum.com/t/activerecord-inheritance-with-types-table/64607/1 "2006-08-15T02:25:13Z")

</div>

I have a Field class:

class Field \< ActiveRecord::Base  
belongs\_to :field\_type  
end

with it’s table:  
fields:  
id  
field\_type\_id  
name  
ordering

every field has a type

class FieldType \< ActiveRecord::Base  
end

stored in the field\_types table  
field\_types:  
id  
name  
class\_name

for each field type, there’s a subclass of Field that do whatever  
special things

class BooleanField \< Field  
def get\_stuff  
return “I am a Boolen Field!”  
end  
end

Is there any way, when I do Field.find(), that I can return fields that  
are all instances of the proper Field subclasses?

I know this could be done using single table inheritance, but I like to  
have the classes in a seperate types table and use foreign keys to to  
ensure there are no invalid types. I also expect to have 10-20 different  
types, all with different attributes that need to be stored in the  
database, so I’d rather not add 20 columns to the fields table. I would  
rather store the attributes as name-value pairs in a seperate table. Any  
ideas would be greatly appreciated.
