# Database design help

**URL:** <https://www.ruby-forum.com/t/database-design-help/133380>\
**Category:** Rails\
**Created:** [March 31, 2008, 2:24am UTC](https://www.ruby-forum.com/t/database-design-help/133380 "2008-03-31T02:24:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 31, 2008, 2:24am UTC](https://www.ruby-forum.com/t/database-design-help/133380/1 "2008-03-31T02:24:12Z")

</div>

Hi, I’m coding a little application to make couples profile. The  
problem is I just don’t know how to create my databases so that their  
are no bottlenecks and that I can have a relation. The way I see it is  
that their would be a user database and a profile database and they  
would link 2 to 1. Is this the right way? In the user db I would store  
their userid and some session info, should I use their userid as  
primary key?

DB Scheme  
User1—  
|—Profile  
User2—

User DB  
-userid (primary key?)  
-profileid  
-session info  
-timestamp

Profile DB  
-personnal info

Any help would be appreciated,  
thanks’ in advance.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 31, 2008, 2:08pm UTC](https://www.ruby-forum.com/t/database-design-help/133380/2 "2008-03-31T14:08:16Z")

</div>

Rails’ convention is that every ActiveRecord object is identified by  
an ‘id’ column (auto-incrementing, int primary key) and foreign keys  
are named \<other\_table\>\_id. The nice thing about following the  
conventions is that it greatly simplifies your coding (in some cases  
eliminating code!). So your model would look something like this:

create\_table :users do |t|  
t.integer :profile\_id  
t.string :crypted\_password  
… etc …  
end

class User \< ActiveRecord::Base  
belongs\_to :profile  
end

create\_table :profiles do |t|  
… personal info …  
end

class Profile \< ActiveRecord::Base  
has\_many :users  
end

With those migrations and classes you do not need to specify the name  
of the primary key (assumed to be id… and written into the  
create\_table migration for you). You also don’t need to tell the  
has\_many macro the name of the foreign key to use when looking up  
users (or the User objects which Profile to file) since it’s assumed  
to be profile\_id.
