Help with model association


#1

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


#2

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

#3

Thanks, I’ll try it.

Petr