Forum: Ruby on Rails Help with model association

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Petr Ruzicka (Guest)
on 2008-12-29 22:25
(Received via mailing list)
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
MaD (Guest)
on 2008-12-30 10: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
...
Petr Ruzicka (Guest)
on 2008-12-30 13:32
(Received via mailing list)
Thanks, I'll try it.

Petr
This topic is locked and can not be replied to.