ruby - Rails DateTime Comparison Bug -
i have ticket
class has default rails column of updated_at
while on ticket page making update polling database see if else has changed @ same time.
in view if works
<p>updated at: <%= @ticket.updated_at.to_time.strftime('%y-%m-%d %h:%m:%s') %></p> <p>current time: <%= time.now.strftime('%y-%m-%d %h:%m:%s') %></p> <% current_time = time.now %> <% if @ticket.updated_at > time.now %> <p>ticket has been updated</p> <% else %> <p>ticket has not been updated</p> <% end %>
so here ajax request (library prototype new to, prefer jquery):
<script type="text/javascript"> function checkticketupdate() { var url = '/ticket/check_ticket_update'; var parameters = 'id=<%= @ticket.id %>¤t_time=<%= current_time %>' var container = 'ticket_updated_container'; var myajax = new ajax.updater(container, url, {method: 'get', parameters: parameters}); settimeout(checkticketupdate, 5000); } checkticketupdate(); </script>
here check_ticket_update
ticket
controller:
def check_ticket_update if params[:id] @ticket = ticket.find_by_id(params[:id].to_i) end updated_at = @ticket.updated_at.to_time.strftime('%y-%m-%d %h:%m:%s') current_time = date.parse(params[:current_time]).strftime('%y-%m-%d %h:%m:%s') if updated_at > current_time render :partial => 'ticket/ticket_updated' end end
this falsely saying ticket has been updated when same test in view says hasn't can't see bug causing behaviour.
update
i updated partial spit out values of each of datetimes:
ticket has been updated updated at: 2013-08-07 16:35:38 # correct datetime database current time: 2013-08-07 00:00:00 # wrong... raw value of params[:current_time]: wed aug 07 20:26:29 +0100 2013
date
for, um, dates. dates have resolution of 1 day there no hours, minutes, ... example:
>> date.parse('2013-08-07 16:35:38').strftime('%y-%m-%d %h:%m:%s') => "2013-08-07 00:00:00"
that means current_time
string has zeros hours, minutes, , seconds. perhaps want use datetime
instead:
>> datetime.parse('2013-08-07 16:35:38').strftime('%y-%m-%d %h:%m:%s') => "2013-08-07 16:35:38"
Comments
Post a Comment