update and save database in a create method using same form rails -
i trying use same form create new database entry, display current db information, , update db information, stuck trying patch in create method within controller. have tried using first_or_initialize , find_or_initialize_by_name, cant seem them work situation. thinking of trying if something.nil? / else type of design. new @ , appreciated!
this controller (working create new db):
def create params[:student].each |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 |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>
thank in advance!
hopefully missing piece. not believe efficiency. explain after.
using attribute existing model following either find or create attendance object student id 10.
attendance.where(:student_id => 10).first_or_create |attendance| attendance.event_id = 3 #arbitrary attendance.attendance_type = 2 #arbitrary end
or
attendance = attendance.where(:student_id => 10).first_or_initialize # bunch of ways set attributes # ... , attendance.save
now, why may inefficient. if going through each student parameter , looping through attribute incur multiple calls. example, 100 students end doing 100 separate queries if loop first_or_initialize on every student attribute set. suggest mapping of parameters require 1 array, doing query , looping through array. put them 1 create call array, , bam cut serious activerecord overhead.
#just giving context student param keys student_ids = params[:student].keys #query student ids attendances = attendance.where(student_id: student_ids) #get list of ids filtering out of existing ids new_student_ids = student_ids - attendances.map{|a| a.student_id} #create array carry hashes attendances = array.new #cycle through of non-created ids , build create list new_student_ids.each |id| attendances << {#assign values hash grabbing them params[:student] using iterating id} end #create bunch @ once attendance.create attendances
Comments
Post a Comment