Search code examples
ruby-on-railsrubyprototypejsruby-on-rails-2

Rails DateTime Comparison Bug


I have a ticket class which has the default rails column of updated_at so while someone is on the ticket page making an update I am polling the database to see if someone else has changed it at the same time.

In the view if I do this it 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 is the ajax request (library is prototype which I am new to, much prefer jQuery):

<script type="text/javascript">
    function checkTicketUpdate() {

        var url        = '/ticket/check_ticket_update';
        var parameters = 'id=<%= @ticket.id %>&current_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 is the check_ticket_update from the 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 is falsely saying that the ticket has been updated when the same test in the view says that it hasn't but I can't see the bug causing this behaviour.

Update

I updated the partial to spit out the values of each of the datetimes:

Ticket has been updated
Updated at: 2013-08-07 16:35:38 # correct datetime from database
Current Time: 2013-08-07 00:00:00 # wrong...
Raw value of params[:current_time]: Wed Aug 07 20:26:29 +0100 2013

Solution

  • Date is for, um, dates. Dates have a resolution of one day so there are no hours, minutes, ... For example:

    >> Date.parse('2013-08-07 16:35:38').strftime('%Y-%m-%d %H:%M:%S')
    => "2013-08-07 00:00:00"
    

    That means that your current_time string has zeros for the hours, minutes, and seconds. Perhaps you want to use a DateTime instead:

    >> DateTime.parse('2013-08-07 16:35:38').strftime('%Y-%m-%d %H:%M:%S')
    => "2013-08-07 16:35:38"