我有Classroom,Instructor,Task和Trainee模型,我试图通过使用多个has_many通过关联的Credit连接模型进行关联。 Credit模型中的每条记录都有一个task_id,trainee_id,instructor_id和classroom_id。
class Classroom < ActiveRecord::Base
has_many :credits, dependent: :destroy
has_many :trainees, through: :credits
has_many :instructors, through: :credits
has_many :tasks, through: :credits
accepts_nested_attributes_for :credits
end
class Instructor < ActiveRecord::Base
has_many :credits
has_many :classrooms, through: :credits
has_many :trainees, through: :credits
has_many :tasks, through: :credits
end
class Task < ActiveRecord::Base
has_many :credits
has_many :classrooms, through: :credits
has_many :trainees, through: :credits
has_many :instructors, through: :credits
end
class Trainee < ActiveRecord::Base
has_many :credits
has_many :classrooms, through: :credits
has_many :instructors, through: :credits
has_many :tasks, through: :credits
end
class Credit < ActiveRecord::Base
belongs_to :classroom
belongs_to :instructor
belongs_to :trainee
belongs_to :task
end
我希望能够通过新的Classrooms表单在我的连接模型中创建多个记录。例如,当我选择一个教师(选择框),三个学生(复选框)和三个任务(复选框)时,我将创建9个记录,每个记录包含task_id,trainee_id,instructor_id和classroom_id。我正在寻找的解决方案将遍历每个培训生和任务,以获得多个完整的记录。
<%= bootstrap_form_for :classroom, url: classrooms_path do |f| %>
<%= f.text_field :title %>
<%= f.text_field :location %>
<%= f.date_select :date %>
<%= f.time_select :start_time, label: "Start Time" %>
<%= f.time_select :end_time, label: "End Time" %>
<%= f.select :instructor_ids, Instructor.all.collect {|x| [x.rank + " " + x.last_name, x.id]}, {} %>
<table class="table table-sm">
<tr>
<th>Trainee</th>
<th>Battery</th>
<th>Crew</th>
<th>Position</th>
<th>Add</th>
</tr>
<% Trainee.all.each do |trainee| %>
<tr>
<td><%= trainee.rank %> <%= trainee.first_name %> <%= trainee.last_name %></td>
<td><%= trainee.battery %></td>
<td><%= trainee.crew %></td>
<td><%= trainee.position %></td>
<td><%= check_box_tag :trainee_ids, trainee.id, @classroom.trainees.include?(trainee), :name => 'classroom[trainee_ids][]' -%></td>
</tr>
<% end %>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
<tr>
<th>Task</th>
<th></th>
<th></th>
<th></th>
<th>Add</th>
</tr>
<% Task.all.each do |task| %>
<tr>
<td><%= task.title %></td>
<td></td>
<td></td>
<td></td>
<td><%= check_box_tag :task_ids, task.id, @classroom.tasks.include?(task), :name => 'classroom[task_ids][]' -%></td>
</tr>
<% end %>
</table>
<%= f.submit "Schedule Training", class: "btn btn-success" %>
到目前为止,我已经成功地将course_id与其他选项联系起来,如下所示。但是,我无法弄清楚如何遍历每个讲师,实习生和任务组合来创建多个完整的记录。
为了完整起见,我在下面添加了我的教室控制器。
class ClassroomsController < ApplicationController
def index
@classrooms = Classroom.all
end
def show
@classroom = Classroom.find(params[:id])
end
def new
@classroom = Classroom.new
end
def edit
@classroom = Classroom.find(params[:id])
end
def create
@classroom = Classroom.new(classroom_params)
if @classroom.save
redirect_to classrooms_path
else
render 'new'
end
end
def update
@classroom = Classroom.find(params[:id])
if @classroom.update(classroom_params)
redirect_to classrooms_path
else
render 'edit'
end
end
def destroy
@classroom = Classroom.find(params[:id])
@classroom.destroy
redirect_to classrooms_path
end
private
def classroom_params
params.require(:classroom).permit(:title, :location, :date, :start_time, :end_time, :instructor_ids, trainee_ids: [], task_ids: [])
end
end
我一直在寻找解决方案,但似乎没有任何东西适合。有没有办法做多个for循环来使这个工作?我认为这会发生在控制器中,但我不确定如何实现这一点。谢谢。