I am trying to record a ‘time_in’, and ‘time_out’ record on a model called ‘timeclock’. Here is what I am trying to do but can’t get to work!
-
Check the last timeclock entry and see if it has both ‘time_in’ and ‘time_out’ records filled in. If so, then create a new record (like this?):
form_for([@employee, @employee.timeclocks.build]) do |f| f.hidden_field :time_in, :value => @current_time f.submit "Clock In" end
-
If there’s already a timeclock record that is incomplete (has a ‘time_in’ record but not a ‘time_out’ record, render a form or update the last record.
I have been able to get the time records into the db, but never on the same data-record, like this: http://cl.ly/E0Rg (image)
Does that make sense? If you need some clarifying questions, don’t hesitate to ask! Thanks!
with the code you present you always create new record as @employee.timeclocks.build is used to create record.
If you want tu update a record you should fetch it first an then pass this record in the form_for method
to fetch a record use @employee.timeclocks.find fappropriate where clause to get the record you want to update
where you set @timeclock in you controller either in with a new record or with the record you want to update