1

Please forgive me if this is a simple question. I've spent some time searching SO and have not fund a clear answer for this question. I'm also new to rails.

I have a regular old many to many relationship with an associative table in between. The relationships are A workout has many exercises An exercise belongs to many workouts.

I have 2 forms, one to create new workouts, and another to create new exercises. The 3rd form will be used to populate the associative table "workout_exercises". This will allow users to add exercises to multiple workouts.

I want a user to pick a workout, where they will be presented with a list of exercises filtered by a category drop down (which I have working). a checkbox selected on the form submission will grab the id's submitted for the exercises. The problem is the ID for the workout is not available from this form.

My models look like this

class Exercise < ActiveRecord::Base
   belongs_to :category
   belongs_to :workout_exercise
end

class Workout < ActiveRecord::Base
   belongs_to :workout_exercise 
end

class WorkoutExercise < ActiveRecord::Base
    has_many :workouts
    has_many :exercises 
end

controller action looks like this

def exercises
      @workout_exercise = WorkoutExercise.new
      respond_to do |format|
         format.js {
            @workout = #?? I don't have the workout ID available from the request
            @exercises = Exercise.where(category_id: params[:id]) #this is for the dropdown filter :id grabed on select change

         }
      end
   end

I was thinking a possible option to this problem might be to use a nested route somehow?

Thanks in advance for any help!

maitland
  • 21
  • 3
  • You should use has_and_belongs_to_many association. Then below thread may help you. http://stackoverflow.com/questions/21688200/rails-4-checkboxes-for-has-and-belongs-to-many-association – Hasmukh Rathod Aug 14 '16 at 05:42
  • please check this too http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association – Breno Perucchi Aug 14 '16 at 06:13
  • Thank you both for the reply. It's not so much the relationship between the models I don't understand, but how to get the workout_id to persist between multiple requests. – maitland Aug 14 '16 at 06:31
  • please look this video https://www.youtube.com/watch?v=c2qwV0B9yfU – Breno Perucchi Aug 14 '16 at 14:34
  • Thank you for the video! This is more or less what I need to do, but it appears that the associative table just magically populates in this video. It looks like the appropriate place to make this assignment might be in the edit action of the controller. – maitland Aug 14 '16 at 16:12
  • I've followd the video you've posted which led me to the same problem, I don't have a workout_id that persists from the show action to the create action in workout_exercises. I have all the selected exercise id's submitted as an array, but I need the workout ID to persist from the show action. – maitland Aug 15 '16 at 04:42

0 Answers0