1

My app consists of exercises that users add to workouts. Users can create exercises or select existing ones.

*** UPDATE **** I've added a model per Ben's solutions. I'm receiving errors as below when attempting to add exercises to workouts. Is my syntax wrong? I've attempted soltions like this:

w=Workout.last
e=Exercise.last

w.exercises.build(:exercise => e)  # NameError: uninitialized constant Workout::ExercisesWorkout
w.exercises_workouts.create(:exericse_id => 1) #NameError: uninitialized constant Workout::ExercisesWorkout

I'm confused by the new methods attached from the association as well as "_" or not, camelCase, Pluralize, symbol..etc.

Rails seems to be looking for the class ExercisesWorkout yet I define "exercises_workouts" and/or ExercisesWorkouts.

Thanks.


I'm having trouble adding exercises to workouts from the rails console. 2 potential issues that I see:

  1. I don't know the proper syntax to do this (build, create, find)
  2. Application setup properly (join table, models,..etc.)

Please let me know where my error is and if there is a better structure / association to use.

Thank you.

Models:

class Exercise < ActiveRecord::Base
    has_many :workouts, :through => :exercises_workouts
    has_many :exercises_workouts
end


class Workout < ActiveRecord::Base
    has_many :exercises, :through => :exercises_workouts
    has_many :exercises_workouts
end

class ExercisesWorkouts < ActiveRecord::Base
    belongs_to :exercise
    belongs_to :workout
end

schema.db:

ActiveRecord::Schema.define(version: 20141129181911) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "exercises", force: true do |t|
    t.string   "name"
    t.string   "description"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "exercises_workouts", id: false, force: true do |t|
    t.integer "exercise_id", null: false
    t.integer "workout_id",  null: false
  end

  create_table "workouts", force: true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

ERROR:

 w=Workout.new #create new workout
 w.name = 'test' #name workout
 w.save #save workout

 e1=Exercise.new #create new exercise
 e1.name='press' #name exercise
 e1.save #save exercise

#I'm not sure of the syntax here... I've tried alot between create, build using symbols and finds...., this is just one example.. 
 w.exercises.create(e1) #NameError: uninitialized constant Workout::ExercisesWorkout
Community
  • 1
  • 1
twinturbotom
  • 1,504
  • 1
  • 21
  • 34
  • 1
    This question seems remarkably similar to this one: http://stackoverflow.com/questions/9643128/rails-model-has-many-through-associations – Ben Lee Nov 29 '14 at 19:51
  • Thanks, I actually had that link in there initially. I thought my case was different because I had no model for the join table. That may appear to be the solution here, while the solution there was related to a table configuration. – twinturbotom Nov 30 '14 at 01:36

1 Answers1

0

You also need a model for the join table:

class ExercisesWorkouts < ActiveRecord::Base
    belongs_to :exercise
    belongs_to :workout
end

Here is answer that covers join tables in more detail, if you're interested:

What would the joining table called in this case in Rails 3.1?

Community
  • 1
  • 1
Ben Lee
  • 52,489
  • 13
  • 125
  • 145
  • Thanks for the direction. I thought I read that the model is not necessarily needed for a join table. I'll test soon. – twinturbotom Nov 30 '14 at 01:39
  • @twinturbotom The model is not needed for `has_and_belongs_to_many`. It is only needed for `has_many :through`. (Of course, the join table in the db is always required) – Ben Lee Nov 30 '14 at 17:58
  • Ben, I think this fixes the association (question 2 of 2) but I'm still receiving errors. Can you address how the syntax should look to add exercises to workouts (question 1 of 2). Thank you for your help here! – twinturbotom Dec 01 '14 at 13:00