# One-to-One relationship -- good idea?

**URL:** <https://www.ruby-forum.com/t/one-to-one-relationship-good-idea/132054>\
**Category:** Rails\
**Created:** [March 16, 2008, 3:18am UTC](https://www.ruby-forum.com/t/one-to-one-relationship-good-idea/132054 "2008-03-16T03:18:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [March 16, 2008, 3:18am UTC](https://www.ruby-forum.com/t/one-to-one-relationship-good-idea/132054/1 "2008-03-16T03:18:33Z")

</div>

I am writing a site that tracks workouts. Thus far I have the following  
models:

User -\> UserExercise (the workout) \<- Exercise

Workouts have two types of exercises – time based (run 4 miles) and  
weight based (bench press 225 lbs 10 times in 3 sets)

to account for this I have two 1 to 1 tables that correspond to  
UserExercise.

WeightBased -\> UserExercise \<- TimeBased

There will be two different forms to update an exercise depending on  
what type of exercise this is.

This seems much cleaner than a UserExercise table that has all the  
fields, only some of which are used.

## Here are my fields: UserExercise

id  
user\_id  
exercise\_id

## WeightBased

user\_exercise\_id  
weight (int)  
reps  
sets

TimeBased  
user\_exercise\_id  
reps  
rep\_distance  
seconds\_elapsed

the first problem is that microsoft access (just for testing, ok)  
requires both 1-to-1 tables to be updated – this makes no sense, but i  
might be missing something and want to check. Also, is it rails practice  
to set the primary key of a one-to-one table to be the primary key of  
the parent table?

thanks for any advice.

tim

---

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [March 16, 2008, 7:14am UTC](https://www.ruby-forum.com/t/one-to-one-relationship-good-idea/132054/2 "2008-03-16T07:14:28Z")

</div>

I think polymorphic relationship is the right solution for ur case.  
Read the rails docs and wiki for more info.

[Http://www.rubyplus.org](Http://www.rubyplus.org)  
Free Ruby & Rails screencasts

On Mar 15, 2008, at 7:18 PM, Tim B.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [March 16, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/one-to-one-relationship-good-idea/132054/3 "2008-03-16T18:53:14Z")

</div>

oops – bad tabbing there – hope this is easier to follow

User ----\> UserExercise \<---- Exercise  
|  
|  
|  
exercise\_interface  
| |  
| has\_one | has\_one  
| :as =\> exercise\_interface | :as =\> exercise\_interface  
| |  
TimeBased WeightBased

- time\_elapsed - reps
- distance - weight

Tim B. wrote:

> I think you might be right, but i am not completely sure polymorphic is  
> best here. If I did use polymorphic my implementation would look like:
> 
> User ----\> UserExercise \<---- Exercise  
> |  
> |  
> |  
> exercise\_interface  
> | |  
> | has\_one | has\_one  
> | :as =\> exercise\_interface | :as =\> exercise\_interface  
> | |  
> TimeBased WeightBased
> 
> - time\_elapsed - reps
> - distance - weight
> 
> This would work well, except my exercise\_type field does not belong in  
> UserExercise, but in Exercise, the table that has the actual  
> descriptions, etc of the exercise. UserExercise records a specific  
> implementation of an exercise (i.e. the data. so naturally, it seems  
> ugly to record the exercise type in UserExercise.
> 
> I have attached my ER diagram (written in access just to play around  
> with these ideas before i implement in mysql)
> 
> but this is great stuff, i am really am learning how this works. thanks  
> for any help.
> 
> best,
> 
> tim

---

<div class="post-metadata">

**Author:** ![tbobker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/73ab20/32.png) [@tbobker](https://www.ruby-forum.com/u/tbobker)\
**Post date:** [March 16, 2008, 6:51pm UTC](https://www.ruby-forum.com/t/one-to-one-relationship-good-idea/132054/4 "2008-03-16T18:51:06Z")

</div>

I think you might be right, but i am not completely sure polymorphic is  
best here. If I did use polymorphic my implementation would look like:

User ----\> UserExercise \<---- Exercise  
|  
|  
|  
exercise\_interface  
| |  
| has\_one | has\_one  
| :as =\> exercise\_interface | :as =\> exercise\_interface  
| |  
TimeBased WeightBased

- time\_elapsed - reps
- distance - weight

This would work well, except my exercise\_type field does not belong in  
UserExercise, but in Exercise, the table that has the actual  
descriptions, etc of the exercise. UserExercise records a specific  
implementation of an exercise (i.e. the data. so naturally, it seems  
ugly to record the exercise type in UserExercise.

I have attached my ER diagram (written in access just to play around  
with these ideas before i implement in mysql)

but this is great stuff, i am really am learning how this works. thanks  
for any help.

best,

tim
