I’m trying to move to rails from traditional web app.
As Rails has some conventions about table schema,
I met some obstacles.
Questions.
This is a sort of general question. Do you make a code table for things
like the following?
Activity Status Codes
01001: Open
01002: Pending
01003: Delayed
01004: Cancelled
01005: Closed
(The first 2 digits are code category.)
Would you make a code table for these codes or would you input the
description directly into the column?
If the codes are flexible (editable by users), the answer must be “use
code table”.
But what if the codes are fixed?
When you need to have a code table, would you still use codes like
‘01001’ or just use a sequential id or both (id is primary key and code
is non-primary key)?
I’d suggest using a database table for this, because then you get to
leverage the active record to help you out with it. Keep in mind though
that
the id column for an active record table is auto incremented. So, if I
were
you I’d create a table called “status_codes” with three columns: “id”,
“category”, “code”, “name”. Note that I split your number into two
columns,
since you said that the first two digits represented something by
themselves. It’s always easier to put data back together than it is to
split
it up.
Then your object that uses these status codes would have a
“status_code_id”
column (referencing the “id” column from “status_codes”), and a “belongs
to
:status_code” declaration in the model class.
Regarding splitting category out into a separate table; There’s nothing
wrong with this idea. However that doesn’t mean you have to do it all at
once. If I were you, I’d try it in one table like the example above, and
then if you decided that categories needed a bit of functionality of
their
own, of if you wanted to reference them seperatelly, of if you found
yourself writing code specific to categories in your StatusCode class,
then
I’d move them to a new table (rails migrations make this very easy.
Activity Status Codes
01001: Open
01002: Pending
01003: Delayed
01004: Cancelled
01005: Closed
Hey Sam-
I would store it in the db, you never know when you will need to add
or remove an option from a configuration table like that. But sine it
is just simple configuration you could make a DBHash that uses the
database for storage but acts like a hash:
class << self
def
pair = find_by_key(key.to_s)
pair.value unless pair.nil?
end
def []=(key, value)
pair = find_by_key(key.to_s)
if pair
pair.value = value
pair.save
else
pair = new
pair.key, pair.value = key.to_s, value
pair.save
end
value
end
def to_hash
Hash[ *find(:all).map { |pair| [pair.key, pair.value] }.flatten ]
end
I’m trying to move to rails from traditional web app.
Activity Status Codes
add or remove an option from a configuration table like that. But
t.column “value”, :string, :default => “”
end
end
Then you can use it like this:
Oops! Configuration should be DBHash below in case i confused you
-Ezra
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.