Skip to main content
added 749 characters in body
Source Link
Pavan
  • 33.6k
  • 8
  • 53
  • 78

The culprit is the workout_day_params. In the form you have the fields of weighted_sets nested under the workout_day. But in the workout_day_params, you have weighted_sets_attributes under exercises_attributes which is the reason for your problem. Changing it to below should solve the issue.

def workout_day_params
  params.require(:workout_day).permit(:title, exercises_attributes: [:title, :_destroy], weighted_sets_attributes: [:id, :weight, :repetition])
end

ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrMany‌​Reflection

This due to wrong associations. You should consider tweaking your associations like below

class WorkoutDay < ApplicationRecord
  has_many :weighted_sets, dependent: :destroy
  has_many :exercises, through: :weighted_sets

  accepts_nested_attributes_for :exercises
  accepts_nested_attributes_for :weighted_sets
end

class Exercise < ApplicationRecord
  has_many :weighted_sets, dependent: :destroy
  has_many :workout_days, through: :weighted_sets
end

class WeightedSet < ApplicationRecord
  belongs_to :exercise, optional: true
  belongs_to :workout_day, optional: true
end

The culprit is the workout_day_params. In the form you have the fields of weighted_sets nested under the workout_day. But in the workout_day_params, you have weighted_sets_attributes under exercises_attributes which is the reason for your problem. Changing it to below should solve the issue.

def workout_day_params
  params.require(:workout_day).permit(:title, exercises_attributes: [:title, :_destroy], weighted_sets_attributes: [:id, :weight, :repetition])
end

The culprit is the workout_day_params. In the form you have the fields of weighted_sets nested under the workout_day. But in the workout_day_params, you have weighted_sets_attributes under exercises_attributes which is the reason for your problem. Changing it to below should solve the issue.

def workout_day_params
  params.require(:workout_day).permit(:title, exercises_attributes: [:title, :_destroy], weighted_sets_attributes: [:id, :weight, :repetition])
end

ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrMany‌​Reflection

This due to wrong associations. You should consider tweaking your associations like below

class WorkoutDay < ApplicationRecord
  has_many :weighted_sets, dependent: :destroy
  has_many :exercises, through: :weighted_sets

  accepts_nested_attributes_for :exercises
  accepts_nested_attributes_for :weighted_sets
end

class Exercise < ApplicationRecord
  has_many :weighted_sets, dependent: :destroy
  has_many :workout_days, through: :weighted_sets
end

class WeightedSet < ApplicationRecord
  belongs_to :exercise, optional: true
  belongs_to :workout_day, optional: true
end
Source Link
Pavan
  • 33.6k
  • 8
  • 53
  • 78

The culprit is the workout_day_params. In the form you have the fields of weighted_sets nested under the workout_day. But in the workout_day_params, you have weighted_sets_attributes under exercises_attributes which is the reason for your problem. Changing it to below should solve the issue.

def workout_day_params
  params.require(:workout_day).permit(:title, exercises_attributes: [:title, :_destroy], weighted_sets_attributes: [:id, :weight, :repetition])
end