I am completely new to rails, and I have a very useful way of dealing
with database structures from my background in php. I don’t want to
be one of those who brings bad habits into rails…so I am curious if
my practices below violate rails’ best practices.
(SQL is below, but I try to explain here for clarity) For example,
let’s say I want a user’s table in the database. I will usually
create it with only necessary information. Name, username, password,
timestamps, etc. Then I set up a “users_profiles” table that is
essentially a table with only three columns (1. A reference to the
user_id, 2. A ‘key’ column (varchar), and 3. A ‘value’ column
(text).). The primary key is a combo of the referenced user_id and
the ‘key’ column. Within my “user” model, I would instantiate an new
“users_profile”, by calling $this->profile = new Users_Profiles().
Then, if I want to set an email, all I have to do is call $this->user-
profile->email = “example email”. Then $this->user->save();. This
technique, allowed for extreme flexibility. Note that any call just
to $this->user->name would have to match the database table column
name, but with the key/value setup in the “users_profiles” table
(using a few setters and getters) I am able to set the “key” to email
without any change to the database.
I appreciated this flexibility and decreased need to alter the
database. Since I see huge advantage in the “Rails
Way”…particularly in ActiveRecord, I would like to adhere to “best-
practices.” Does anyone know if this would be considered a poor way
of managing data in the “rails way”. It seems to me that it would
function similarly, in that it would save a lot of updates with the db-
migrations.
Thank you in advance for your help!
Andrew P.
Here are the actual SQL statements for a real site.
create table users (
user_id serial not null,
username varchar(255) not null,
password varchar(40) not null,
user_type varchar(20) not null,
ts_created datetime not null,
ts_last_login datetime,
primary key (user_id),
unique (username)
)
type=InnoDB;
create table users_profile (
user_id bigint unsigned not null,
profile_key varchar(255) not null,
profile_value text not null,
primary key (user_id, profile_key),
foreign key (user_id) references users (user_id)
)
type=InnoDB;