Background: I've a credit card applicatoin with sports athletes, all these sports athletes (Athlete model) could be designated workout routines (Workout model, workout_assignment through model).
Problem: In the beginning I figured that utilizing a through reference to sports athletes getting many workout routines through workout_assignment and workout routines getting many sports athletes through workout_assignment works acceptable for me. Basically made it happen by doing this however, basically designated a good work out to 50 sports athletes, they'd be referencing exactly the same workout record. I would like a coach (the one who assigns workout routines) to have the ability to assign exactly the same workout to 50 sports athletes however have the ability to change them 1 by 1 if he wishes (personalize for that sports athletes). Does anybody have advice for me personally regarding how to approach this? Should i create 50 copies from the workout and assign each to some user, will i actually need the workout_assignment through model then basically have separate workout routines?
Appreciate any advice you are able to give!
create_table "athletes", :force => true do |t| t.string "name" t.string "username" t.string "password" t.string "sport" t.text "notes" t.integer "coach_id" t.datetime "created_at" t.datetime "updated_at" t.string "crypted_password" t.string "password_salt" t.string "persistence_token" end create_table "coaches", :force => true do |t| t.string "name" t.string "username" t.string "password" t.string "address" t.string "city" t.string "state" t.string "zipcode" t.string "phone" t.string "sports" t.integer "experience" t.datetime "created_at" t.datetime "updated_at" t.string "crypted_password" t.string "password_salt" t.string "persistence_token" end create_table "workout_assignments", :force => true do |t| t.integer "athlete_id" t.integer "workout_id" t.date "date_assigned" t.datetime "created_at" t.datetime "updated_at" end create_table "workouts", :force => true do |t| t.string "name" t.string "type" t.integer "coach_id" t.text "description" t.datetime "created_at" t.datetime "updated_at" t.integer "category_id" end
class Athlete < ActiveRecord::Base belongs_to :coach has_many :workouts, :through => :workout_assignments end class Workout < ActiveRecord::Base has_many :athletes, :through => :workout_assignments belongs_to :category end class WorkoutAssignment < ActiveRecord::Base belongs_to :workout belongs_to :athlete end class Coach < ActiveRecord::Base has_many :athletes has_many :workouts end
The main reason to utilize a has and goes to relationship (via reciprocal :through relationship or else) would be to reuse the objects on each side.
Have you thought about utilizing a wealthy join model? Your exercise routine_projects model already appears to become one because of the "date_designated" column. The idea is your join model includes data specific to that particular assignment. This way you will simply need one copy of every common workout element, and employ the workout projects model to keep the person instructions.
Here's how I recommend applying this:
Adapt workout_projects to incorporate special instructions for that athlete. All that you should do is give a column towards the workout_projects table, I am likely to refer to it as
special_instructions however, you can refer to it as no matter what you would like.
Within the up approach to a brand new migration:
add_column :workout_assignments, :special_instructions, :string
That's about this. No be sure that you display the special instructions additionally towards the workout's description when exhibiting it for an athlete/coach. You will find somethings that you can do to alleviate the transition of mentioning to workout_projects instead of workout routines.
Take a look into delegation, custom accessors and use of the :include option on the has_many association. For inspiration regarding how to make these models are more effective together. The concept is you can eager load the connected workout when you load a good work out assignment.
Delegation is really a technique that enables you to definitely pass techniques with other options. Essentially, giving the look of merging the 2 models. In it's easiest form it appears something similar to this:
def description workout.description end
Then write a the custom accessor for description on workout_projects that brings the description for that the connected exercise and shows it with the special instructions from the assignment.