Ruby on rails 具有多个通过或具有并属于多个缺少的ID

Ruby on rails 具有多个通过或具有并属于多个缺少的ID,ruby-on-rails,has-many-through,has-and-belongs-to-many,Ruby On Rails,Has Many Through,Has And Belongs To Many,只是想在这里创建一个简单的训练日志,当我尝试创建重量时,会出现这个错误 “训练ID必须存在” 模型 class Workout < ApplicationRecord belongs_to :user has_many :exercises has_many :weights, through: :exercises accepts_nested_attributes_for :exercises, :allow_destroy => true accepts_nested

只是想在这里创建一个简单的训练日志,当我尝试创建重量时,会出现这个错误

“训练ID必须存在”

模型

class Workout < ApplicationRecord
 belongs_to :user
 has_many :exercises
 has_many :weights, through: :exercises
 accepts_nested_attributes_for :exercises, :allow_destroy => true
 accepts_nested_attributes_for :weights, :allow_destroy => true
 validates :bodypart, presence: true, length: { maximum: 255 }
end

class Weight < ApplicationRecord
  belongs_to :exercise
  belongs_to :workout
  validates :amount, presence: true, length: { maximum: 255 }
  validates :reps, presence: true, length: { maximum: 255 }
end

class Exercise < ApplicationRecord
  belongs_to :workout
  has_many :weights
  accepts_nested_attributes_for :weights
  validates :name, presence: true
  validates_associated :weights
end
演习控制员

class ExercisesController < ApplicationController
 before_action :authenticate_user!
 # before_action :set_exercise, except: [:index, :new, :create]

def new
 @exercise = Exercise.new
 @exercise.weights.new
end

def index
 @workout = Workout.find(params[:workout_id])
 @exercise = @workout.exercises
end

def create
 @workout = Workout.find(params[:workout_id])
 @exercise = @workout.exercises.build(exercise_params)
   if @exercise.save
     redirect_to workout_exercise_path(@workout, @exercise)
     flash[:notice] = "Exercise created, enter weight and reps for      your first set"
  else 
    redirect_to @workout
    flash[:notice] = "#{@exercise.errors.full_messages.to_sentence}"
  end
 end

def edit
end

def update
 @exercise = Exercise.find(params[:id])
 if @exercise.update_attributes(exercise_params)
   redirect_to exercise_path(@exercise)
 else
  flash[:notice] = "Something went wrong"
 end
end

def show
 @exercise = Exercise.find(params[:id])
 @weights = @exercise.weights.order('created_at DESC').paginate(page:     params[:page], per_page: 5)
end
class ExercisesController

我的权重模型上确实设置了锻炼ID的ID列,但只要我能够创建锻炼,它总是填充NULL。另外,在rails控制台中,我可以找到一个训练并调用@workout.weights,它会很好地返回与训练相关的权重。我只是无法在重量模型中填充训练ID。想知道是否拥有并属于许多人会更好,或者我的“拥有许多人通过”只是设置错误。我真的尝试过“倒数”但没有任何运气。任何帮助都将不胜感激。

答案很简单。要添加反向功能,您必须通过练习显式地声明它

weight.rb
中,删除
所属内容\u:workout
并添加以下内容

def workout
    self.exercise.workout
end
也许您需要添加逻辑以避免类错误

现在您已经通过
@weight.exercise
完成了
@weight.exercise

class ExercisesController < ApplicationController
 before_action :authenticate_user!
 # before_action :set_exercise, except: [:index, :new, :create]

def new
 @exercise = Exercise.new
 @exercise.weights.new
end

def index
 @workout = Workout.find(params[:workout_id])
 @exercise = @workout.exercises
end

def create
 @workout = Workout.find(params[:workout_id])
 @exercise = @workout.exercises.build(exercise_params)
   if @exercise.save
     redirect_to workout_exercise_path(@workout, @exercise)
     flash[:notice] = "Exercise created, enter weight and reps for      your first set"
  else 
    redirect_to @workout
    flash[:notice] = "#{@exercise.errors.full_messages.to_sentence}"
  end
 end

def edit
end

def update
 @exercise = Exercise.find(params[:id])
 if @exercise.update_attributes(exercise_params)
   redirect_to exercise_path(@exercise)
 else
  flash[:notice] = "Something went wrong"
 end
end

def show
 @exercise = Exercise.find(params[:id])
 @weights = @exercise.weights.order('created_at DESC').paginate(page:     params[:page], per_page: 5)
end
def workout
    self.exercise.workout
end