Forum: Ruby on Rails Help with model association

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
C123314b61d5cd29612dcefe68eaedca?d=identicon&s=25 Petr Ruzicka (Guest)
on 2008-12-29 21:25
(Received via mailing list)
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

class Training < ActiveRecord::Base
    belongs_to :workouts
    belongs_to :trainee

class Workouts < ActiveRecord::Base
    has_many :trainings
    has_many :trainees, :through => :trainings

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

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

Petr Ruzicka
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2008-12-30 09:56
(Received via mailing list)
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
C123314b61d5cd29612dcefe68eaedca?d=identicon&s=25 Petr Ruzicka (Guest)
on 2008-12-30 12:32
(Received via mailing list)
Thanks, I'll try it.

This topic is locked and can not be replied to.