Rails and Code Tables

R

rmmcgr

Hi,

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?

Thanks for any advice/help,
Regards,
Richard
 
D

Daryl Richter

rmmcgr said:
Hi,

[snip]


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.
 
T

Tom Wilcoxen

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.

-Tom

--=20
Tom Wilcoxen
http://convergentarts.com
http://www.dreamhost.com/r.cgi?twilcoxen
 
P

Paul

Has to be something like this

create table codes(
id integer not null auto-increment .....

create table addresses(
id integer not null auto-increment,
code_id integer --ref to codes id col
.....

class Code < ActiveRecord::Base
has_many :addresses
end

class Address < ActiveRecord::Base
belongs_to :code
end
 
R

rmmcgr

Thanks for the ideas. That gives me some great pointers to figure
something out.

Regards,
Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top