I am new to Ruby and the Ruby on Rails framework, and I am in the
process of learning both.
One question I have about Rails, and ActiveRecord, is: Is there a way
to get ActiveRecord to understand the use of a code table?
Many of the applications I have developed have a code table that would
have a code_type column, a code_value column and a description column.
Another table that references a code value, of a particular code type,
might have a name like address_type_code. So that in a user interface
I can populate a list of options based on the code type (e.g.
"ADDRESSTYPES"), and store in address_type_code the corresponding code
value selected (e.g. "HOME").
Some tables have multiple columns where they all reference the code
table, but for differing code types. My experience with this has been
to have one table (the code table) to store many options like this,
rather than to have lots of little tables that are for situations such
as storing a list of available options.
So I guess my question is two fold. Is this a good practise in the
first place? And secondly how would I go about getting rails, and
ActiveRecord, to work with a code table?
Some tables have multiple columns where they all reference the code
table, but for differing code types. My experience with this has been
to have one table (the code table) to store many options like this,
rather than to have lots of little tables that are for situations such
as storing a list of available options.
So I guess my question is two fold. Is this a good practise in the
first place? And secondly how would I go about getting rails, and
ActiveRecord, to work with a code table?
This sounds to me like an unwise db schema design, but it's hard to tell without at least a small specific example of what you're doing.
···
Thanks for any advice/help,
Regards,
Richard
--
Daryl
"We want great men who, when fortune frowns, will not be discouraged."
-- Colonel Henry Knox, 1776
If I follow, then you're db would look something like:
create table code(
code varchar(15) not null,
value varchar(40) not null,
type varchar(15) not null
)
If you did that you could use single table inheritance I think. You'd
have a main Code ActiveRecord object, then a specific one for each
type, e.g. Address < Code. Then you could use those AR objects to get
the types you need.
If you use the above you'll need to declare code to be your primary
key in your AR object and you'd want it to be unique across your
types.