Ruby on rails rails:更新嵌套在两个联接表中的记录

Ruby on rails rails:更新嵌套在两个联接表中的记录,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我有个问题让我很困惑 我有四个模型,一个锻炼模型,一个锻炼模型,一个锻炼-锻炼-加入模型和一个锻炼-锻炼-设置模型 我可以通过“训练\练习加入”表将练习添加到训练中。现在我正在尝试在训练中为练习添加集合,这就是“训练集”表的作用 这里有一个例子 Workout Exercise 1 Set 1 Set 2 Set 3 Exercise 2 Set 1 Set 2 Set 3 Exercise 3 etc. 在

我有个问题让我很困惑

我有四个模型,一个锻炼模型,一个锻炼模型,一个锻炼-锻炼-加入模型和一个锻炼-锻炼-设置模型

我可以通过“训练\练习加入”表将练习添加到训练中。现在我正在尝试在训练中为练习添加集合,这就是“训练集”表的作用

这里有一个例子

Workout   
  Exercise 1
    Set 1
    Set 2
    Set 3   
  Exercise 2
    Set 1
    Set 2
    Set 3   
  Exercise 3
    etc.
workouts/edit.html.erb
中,我有一个表格,可以在其中查看训练中的所有练习,并使用
form\u对
fields\u对
编辑套数,但当我尝试更新一项训练或整个训练时,我会得到一个
MassAssignmentSecurity::workoutscoontroller中的错误#update
,它表示
不能批量分配受保护的属性:workout\u exercise\u set
。我想不出怎么过去。我知道我正在编辑一个训练,但我并没有试图写入一个名为
workout\u exercise\u set
的字段,所以我在这里真的很困惑。我真的很感激任何指导。所有相关代码如下

workout/edit.html.erb:

<%= form_for(@workout) do |f| %>
    <%= f.label :name %><br />
    <%= f.text_field :name %><br />
    <%= f.label :description %><br>
    <%= f.text_area :description %></br>
        <%= f.fields_for :workout_exercises do |s| %>
                <%= s.object.exercise.name %></b>
                    <%= s.fields_for :workout_exercise_sets do |set| %> 
                        <%= set.label :set_number %>:
                        <%= set.number_field :set_number %>     
                        <%= set.label :reps %>:
                        <%= set.number_field :repetitions %>
                        <%= set.label :rest_time %>(seconds):
                        <%= set.number_field :rest_time %>
                        <%= set.submit %>
                    <% end %>
                <%= s.hidden_field :_destroy %>
                <%= link_to "Remove exercise?", '#', class: "remove_fields" %>
        <% end %>
    <%= f.submit %>
<% end %>





: : (秒):
以下是训练模式:

class Workout < ActiveRecord::Base
  attr_accessible :name, :exercises_attributes, :workout_exercises_attributes, :exercise_order, :description

  has_many :workout_exercises, dependent: :destroy, :order => "exercise_order DESC"
  has_many :exercises, through: :workout_exercises

  accepts_nested_attributes_for :exercises
  accepts_nested_attributes_for :workout_exercises, allow_destroy: :true

end
课堂训练“锻炼锻炼顺序说明”
有很多:锻炼,通过:锻炼
接受\u嵌套的\u属性\u用于:练习
接受\u嵌套的\u属性\u用于:训练\u练习,允许\u销毁::true
结束
以下是练习模式:

class Exercise < ActiveRecord::Base
  attr_accessible :name, :description

  has_many :workout_exercises
  has_many :workouts, through: :workout_exercises

  validates :name,        uniqueness: :true, presence: :true
  validates :description, uniqueness: :true, presence: :true

end
class WorkoutExercise < ActiveRecord::Base
  attr_accessible :exercise_id, :workout_id

  belongs_to :exercise
  belongs_to :workout

  has_many :workout_exercise_sets, dependent: :destroy 
  accepts_nested_attributes_for :workout_exercise_sets, allow_destroy: :true

end
课堂练习
以下是锻炼模式:

class Exercise < ActiveRecord::Base
  attr_accessible :name, :description

  has_many :workout_exercises
  has_many :workouts, through: :workout_exercises

  validates :name,        uniqueness: :true, presence: :true
  validates :description, uniqueness: :true, presence: :true

end
class WorkoutExercise < ActiveRecord::Base
  attr_accessible :exercise_id, :workout_id

  belongs_to :exercise
  belongs_to :workout

  has_many :workout_exercise_sets, dependent: :destroy 
  accepts_nested_attributes_for :workout_exercise_sets, allow_destroy: :true

end
class-WorkoutExercise
最后,这里是锻炼集模型:

class WorkoutExerciseSet < ActiveRecord::Base
  attr_accessible :repetitions, :rest_time, :set_number, :workout_exercise_id

  belongs_to :workout_exercise
end
class WorkoutExerciseSet

为了更好地衡量,这里有一个DB图:

我认为在你的
锻炼锻炼.rb
文件中,你应该将
:锻炼锻炼锻炼设置属性添加到你的
属性列表中

class WorkoutExercise < ActiveRecord::Base
  attr_accessible :exercise_id, :workout_id, :workout_exercise_sets_attributes

  belongs_to :exercise
  belongs_to :workout

  has_many :workout_exercise_sets, dependent: :destroy 
  accepts_nested_attributes_for :workout_exercise_sets, allow_destroy: :true

end
class-WorkoutExercise
我的解决方案不管用吗?好主意。很遗憾,它没有更改错误消息。与此相关的一点是,这行代码
接受\u嵌套的\u属性\u用于:锻炼\u锻炼\u集合,允许[u destroy::true
workoutExercise.rb
中导致
字段[u for:workout\u exercise\u set do | set |
出于某种原因不输出任何内容。您是否使用
build
创建嵌套元素?我认为您最好签出此页面:您必须
构建
嵌套属性,以便它们出现在表单中。它我犯了个愚蠢的错误。我还没有将任何记录放入表中,因此没有看到任何输出。作为将来的参考,嵌套表单上有一个很棒的railscast。