# Help with model association

**URL:** <https://www.ruby-forum.com/t/help-with-model-association/156546>\
**Category:** Rails\
**Created:** [December 29, 2008, 9:25pm UTC](https://www.ruby-forum.com/t/help-with-model-association/156546 "2008-12-29T21:25:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Petr\_Ruzicka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@Petr\_Ruzicka](https://www.ruby-forum.com/u/Petr_Ruzicka)\
**Post date:** [December 29, 2008, 9:25pm UTC](https://www.ruby-forum.com/t/help-with-model-association/156546/1 "2008-12-29T21:25:30Z")

</div>

Hi,  
I’m very new to RoR so my question may be bit stupid.

I’m trying to create application that could serve as training log. So  
I have “Trainee” who goes to a gym. What he/she does is a  
“Workout” (and there is a bunch of them in list to choose from).  
Now there are many Trainees in a gym at a same time having different  
(or same possibly) Workout.  
So I have a table with Trainees, table with Workouts and I would like  
to join them in Trainings ( Trainee id\_1 did Workout id\_2, Trainee  
id\_2 did Workout id\_4, both at 6 pm on 28.12.2008). One Training has  
many Trainees each with different or same Workout. Trainees and  
Workouts are connected together by join table with one additional  
value of date and time of training.  
At the beginning my idea was this

class Trainee \< ActiveRecord::Base  
has\_many :trainings  
has\_many :workouts, :through =\> :trainings  
end

class Training \< ActiveRecord::Base  
belongs\_to :workouts  
belongs\_to :trainee  
end

class Workouts \< ActiveRecord::Base  
has\_many :trainings  
has\_many :trainees, :through =\> :trainings  
end

But I don’t want to create new Trainee with new Workouts, both would  
be more or less stable (list of Trainees and list of Workouts to  
choose from). I would like to add new Training, choose date&time,  
choose Trainees (one or more) and attach Workouts to them (same or  
different to each) (so multiple Trainees on one Training, each Trainee  
with one his own Workout).

My queries should be something like

- who did Workout\_id1 with best time
- which Workouts Trainee\_id2 did in last 30 days
- who attended Training on 28.12.2008  
etc.

Is my association model correct ? Could I use join table directly like  
this ?.  
Thank you very much

Petr Ruzicka

---

<div class="post-metadata">

**Author:** ![Petr\_Ruzicka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@Petr\_Ruzicka](https://www.ruby-forum.com/u/Petr_Ruzicka)\
**Post date:** [December 30, 2008, 9:56am UTC](https://www.ruby-forum.com/t/help-with-model-association/156546/2 "2008-12-30T09:56:48Z")

</div>

of course you can do it like this. you have acertain amount of  
workouts and trainees and add a new training every time you want. your  
models seem correct.

your request would be like:

- Workout.find(:first, :order =\> :elapsed\_time).trainee  
…

---

<div class="post-metadata">

**Author:** ![Petr\_Ruzicka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@Petr\_Ruzicka](https://www.ruby-forum.com/u/Petr_Ruzicka)\
**Post date:** [December 30, 2008, 12:32pm UTC](https://www.ruby-forum.com/t/help-with-model-association/156546/3 "2008-12-30T12:32:06Z")

</div>

Thanks, I’ll try it.

Petr
