I'm trying to customize my relationships, but I'm having problems using associations.
So, I have three Workout models, Exercise and WorkoutExercise . A workout should have a lot of exercises, and the exercise should have different workouts, so I wrote:
class Workout < ActiveRecord::Base has_many :workout_exercises has_many :exercises, :through => :workout_exercises end class Exercise < ActiveRecord::Base has_many :workout_exercises has_many :workouts, :through => :workout_exercises end class WorkoutExercise < ActiveRecord::Base belongs_to :exercise belongs_to :workout end
I run some tests, but the tests fail as soon as I create a workout, exercise, and then join them in the workout_exercise class. This will not allow me to access the exercises in the workout as follows:
Workout.create Exercise.create WorkoutExercise.create(:workout => Workout.first, :exercise => Exercise.first) work = Workout.first work.exercises.count
My database tables are as follows:
class CreateWorkouts < ActiveRecord::Migration def change create_table :workouts do |t| t.string :title t.text :description t.float :score t.timestamps end end end class CreateExercises < ActiveRecord::Migration def change create_table :exercises do |t| t.string :title t.text :description t.float :value t.timestamps end end end class CreateWorkoutExercises < ActiveRecord::Migration def change create_table :workout_exercises do |t| t.timestamps end end end
When I run these tests, he says that the exercises are undefined. Does anyone have any idea?
trev9065
source share