我正在尝试使用相同的表单来创建新的数据库条目,显示当前的数据库信息,现在更新数据库信息,但是我很难尝试在控制器内的create方法中进行修补。我尝试过使用first_or_initialize和find_or_initialize_by_name,但似乎无法让它们适应这种情况。我在考虑尝试if something.nil? / else设计类型。我是新来的,任何帮助表示赞赏!
这是我的控制器(目前正在创建新数据库):
def create
params[:student].each do |student_id, attendance_type|
attendance = Attendance.new
attendance.attendance_type = attendance_type.to_i
attendance.student_id = student_id
attendance.event_id = params[:event_id]
attendance.save
end
redirect_to :back
end
HTML:
<tbody>
<% @students.each do |student| %>
<form class="form-group" action="/events/<%= @event.id %>/attendances" method="post">
<tr>
<div class="checkbox">
<td style="white-space:normal;"><%= link_to student.name, student_path(student.id) %></td>
<% attendance = student.attendances.find_by_event_id(@event.id) %>
<% if attendance.blank? || attendance.attendance_type == 1 %>
<td style="white-space:normal;"><label class="radio-inline"><input type="radio" name="student[<%= student.id %>]" checked="checked" id="inlineRadio1" value="1"></label></td>
<td style="white-space:normal;"><label class="radio-inline"><input type="radio" name="student[<%= student.id %>]" id="inlineRadio2" value="2"></label></td>
<td style="white-space:normal;"><label class="radio-inline"><input type="radio" name="student[<%= student.id %>]" id="inlineRadio3" value="3"></label></td>
<% elsif attendance.attendance_type == 2 %>
<td style="white-space:normal;"><label class="radio-inline"><input type="radio" name="student[<%= student.id %>]" id="inlineRadio1" value="1"></label></td>
<td style="white-space:normal;"><label class="radio-inline"><input type="radio" name="student[<%= student.id %>]" checked="checked" id="inlineRadio2" value="2"></label></td>
<td style="white-space:normal;"><label class="radio-inline"><input type="radio" name="student[<%= student.id %>]" id="inlineRadio3" value="3"></label></td>
<% else attendance.attendance_type == 3 %>
<td style="white-space:normal;"><label class="radio-inline"><input type="radio" name="student[<%= student.id %>]" id="inlineRadio1" value="1"></label></td>
<td style="white-space:normal;"><label class="radio-inline"><input type="radio" name="student[<%= student.id %>]" id="inlineRadio2" value="2"></label></td>
<td style="white-space:normal;"><label class="radio-inline"><input type="radio" name="student[<%= student.id %>]" checked="checked" id="inlineRadio3" value="3"></label></td>
<% end %>
</div>
<% end %>
</tr>
<tr><%= button_to 'Submit', :class => 'btn btn-primary' %></tr>
</form>
</tbody>
提前谢谢!
答案 0 :(得分:0)
希望这是你失踪的一块。我认为这不会有助于提高效率。我会在之后解释。
使用已存在的模型属性,我会执行以下操作,为ID为10的学生查找或创建出勤对象。
Attendance.where(:student_id => 10).first_or_create do |attendance|
attendance.event_id = 3 #arbitrary
attendance.attendance_type = 2 #arbitrary
end
OR
attendance = Attendance.where(:student_id => 10).first_or_initialize
# a bunch of ways to set attributes
# ... and then
attendance.save
现在,为什么这可能效率低下。如果您浏览每个学生参数并循环浏览某些属性,则会产生多个调用。例如,对于100名学生,如果您在每个学生属性集上循环使用first_or_initialize,则最终将执行100个单独的查询。我建议将所需的所有参数一起映射到一个数组中,执行where查询然后循环遍历数组。将它们全部放入一个创建调用作为一个数组,BAM你自己削减了一些严重的ActiveRecord开销。
#Just giving some context to the student param keys
student_ids = params[:student].keys
#Query for all student ids
attendances = Attendance.where(student_id: student_ids)
#Get a list of ids by filtering out all of the existing ids
new_student_ids = student_ids - attendances.map{|a| a.student_id}
#Create an array to carry your hashes
attendances = Array.new
#Cycle through all of your non-created ids and build you create list
new_student_ids.each do |id|
attendances << {#assign all values as a hash by grabbing them from params[:student] using the iterating id}
end
#Create a bunch at once
Attendance.create attendances