Skip to content

Instantly share code, notes, and snippets.

@mrnugget
Created April 7, 2012 14:29
Show Gist options
  • Save mrnugget/2329363 to your computer and use it in GitHub Desktop.
Save mrnugget/2329363 to your computer and use it in GitHub Desktop.
Wat?
# While looking at a page, I create a comment in the background:
irb(main):005:0> Time.now
=> 2012-04-07 16:27:49 +0200
irb(main):006:0> new_comment = Comment.create!(user: User.first, song: Song.last, text: "Hello3")
User Load (0.3ms) SELECT "users".* FROM "users" LIMIT 1
Song Load (0.2ms) SELECT "songs".* FROM "songs" ORDER BY "songs"."id" DESC LIMIT 1
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "comments" ("created_at", "song_id", "text", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?) [["created_at", Sat, 07 Apr 2012 14:27:51 UTC +00:00], ["song_id", 95], ["text", "Hello3"], ["updated_at", Sat, 07 Apr 2012 14:27:51 UTC +00:00], ["user_id", 1]]
(2.7ms) commit transaction
=> #<Comment id: 32, user_id: 1, text: "Hello3", created_at: "2012-04-07 14:27:51", updated_at: "2012-04-07 14:27:51", song_id: 95>
# Now I reload the page, and I should get an indicator telling me that there is a new comment... From the server output, the query that looks for the new comments since my last activity (saved in a cookie as timestamp):
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
(0.1ms) SELECT COUNT(*) FROM "comments" WHERE (created_at > '2012-04-07 16:27:32 +0200')
CACHE (0.0ms) SELECT COUNT(*) FROM "comments" WHERE (created_at > '2012-04-07 16:27:32 +0200')
#
#
#
# All this was done in a timeframe of 20 seconds. The last cookie was set at 16:27:32 +0200, the comments where created at 14:27:51 - which should be 16:27:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment