有许多通过或拥有并属于许多缺失的ID

时间:2017-01-17 19:16:01

标签: 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_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

在阅读了一些事情之后,我认为有很多东西可以作为这些协会的选择,但现在我不太确定。当我尝试为锻炼创造一个体重时,我得到锻炼ID,但是尽管经过多次尝试但似乎无法获得锻炼ID。我不确定它是否仅仅是一个控制器问题,或者我是否遗漏了更大的东西。

我的当前权重控制器

class WeightsController < ApplicationController
 before_action :authenticate_user!

def new
 @weight = Weight.new
end

 def create
   @exercise = Exercise.find(params[:exercise_id])
   @weight = @exercise.weights.build(weight_params)
 if @weight.save
   flash[:notice] = "Set saved"
   redirect_to exercise_path(@exercise)
 else
   redirect_to exercise_path(@exercise)
   flash[:notice] = "#{@weight.errors.full_messages.to_sentence}"
 end
end

def edit
end

def update
end

def destroy
 weight = Weight.find(params[:id])
 @weight.destroy
 redirect_to root_path
end

private

def weight_params
 params.require(:weight).permit(:amount, :reps)
end

end

的routes.rb

Rails.application.routes.draw do

devise_for :users, controllers: { omniauth_callbacks: "callbacks" }
root 'articles#index'

get '/demo', to: 'static_pages#demo'
get '/about', to: 'static_pages#about'
get '/test', to: 'static_pages#index'

resources :articles

resources :workouts do
    resources :exercises
end

resources :exercises do
    resources :weights
end

resources :workouts do
    member do
        put :is_finished
        put :unfinish
    end
end

resources :exercises do
    member do
        put :is_finished
        put :unfinish
    end
end

resources :books, :only => :index
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

我在Weights模型上设置了workout_id的ID列,但只要我能设法创建练习,它就会填充NULL。此外,在rails控制台中,我可以找到锻炼并呼叫@ workout.weights,它会返回与锻炼相关的权重。我只是无法在体重模型中填充锻炼ID。想知道是否有并且属于许多人会更好或者如果我有很多通过只是设置错误。我没有运气就尝试了“inverse_of”。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

答案很简单。要添加反向功能,您必须通过练习明确说明它。

weight.rb中移除belongs_to :workout并添加以下内容

def workout
    self.exercise.workout
end

也许你需要添加逻辑来避免nilClass错误。

现在你有@weight.workout@weight.exercise