I have full control over the database, and if my app is successful, it
will replace the existing legacy application (written in PHP).
I’d basically come to the same conclusion as you had - what I had in
A table called ‘Tables’ or ‘Research_Variables’.
It would have at the least the following fields:
‘id’ (PK), ‘table_name’, ‘foreign_key_name’.
So basically whenever a new table is created, it will be added to this
table. Perhaps I won’t be able to take full advantage of rails’
ActiveRecord if I do this, and might have to most of my querying through
I don’t know how much control you have over the database, but since
you are able to add tables, on the fly at that (scary), I am assuming
you have full control. I also don’t know if you have legacy
applications hitting this database, if you do how do they find the new
There are two solutions for this, add a table that handles choice
types and add a foreign key relationship to choices, or just put the
choice type name in the table. The second solution isn’t normalized,
so I would suggest the first.
So… Create a table choice_types with id and name columns. Then
create another table choices with id, choice_type_id, value columns.
In your ActiveRecord you will want to tell choice_type that it has
many choices and tell the choice AR that it belongs to choice_types.
Now you can interate over all of your choice_types to build a
different selector, and then use choice_type.choices to retrieve all
of the choices for that choice_type.
This way you don’t have to create a table for each new choice type,
you just have to add a row to the choice_type table. You can then add
a bunch of choices to your choices table for that new choice_type.
Good luck, I hope that helps.
BTW… I am new to Ruby and Rails so please forgive me if I got some
of the syntax incorrect.