ActiveRecord inheritance with types table

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs