views:

31

answers:

1

I have a user table in postgress. Each user can have from 0 to many websites.

I know that it will be waste of memory to bring the user-websites everytime I get the user object from the database and of course I cant know how many websites the user will have.

I could have a table called websites but then I think this could happen again with other sorts of lists that I want to add under the user profile.

What is the best solution for this problem?

Note: Best meaning a solution that will not affect the performance of the website. FYI : the website will be running on ruby on rails 3

+1  A: 

You can have something like this:

create table users (
  user_id serial primary key,
  username text not null unique
);
create table datatypes (
  datatype_id serial primary key,
  datatype text not null unique
);
create table data (
  user_id int not null references users(user_id),
  datatype_id int not null references datatypes(datatype_id),
  data text not null
);
insert into datatypes (datatype)
  values ('website','interest','contact_number');

Then add a website address 'example.com' to user 'testuser':

insert into data (user_id, datatype_id, data)
  select user_id, datatype_id, 'example.com'::text as data
  from users, datatypes
  where username='testuser' and datatype='website';
Tometzky
I wish I could vote up :)4 more to go. Thank you