One-to-One relationship -- good idea?

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

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

Http://www.rubyplus.org
Free Ruby & Rails screencasts

On Mar 15, 2008, at 7:18 PM, Tim B.
<[email protected]

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

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