# database design help

**URL:** <https://discuss.rubyonrails.org/t/database-design-help/27246>\
**Category:** rubyonrails-talk\
**Created:** [March 31, 2008, 12:23am UTC](https://discuss.rubyonrails.org/t/database-design-help/27246 "2008-03-31T00:23:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lpgauth](https://avatars.discourse-cdn.com/v4/letter/l/43a26b/32.png) [@lpgauth](https://discuss.rubyonrails.org/u/lpgauth)\
**Post date:** [March 31, 2008, 12:23am UTC](https://discuss.rubyonrails.org/t/database-design-help/27246/1 "2008-03-31T00:23:29Z")

</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--- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>---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:** ![AndyV1](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@AndyV1](https://discuss.rubyonrails.org/u/AndyV1)\
**Post date:** [March 31, 2008, 12:07pm UTC](https://discuss.rubyonrails.org/t/database-design-help/27246/2 "2008-03-31T12:07:48Z")

</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| &nbsp;&nbsp;t.integer :profile\_id &nbsp;&nbsp;t.string :crypted\_password &nbsp;&nbsp;... etc ... end

class User \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :profile end

create\_table :profiles do |t| &nbsp;&nbsp;... personal info ... end

class Profile \< ActiveRecord::Base &nbsp;&nbsp;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.
