Skip to content

Instantly share code, notes, and snippets.

@ryanwood
Last active August 29, 2015 14:23
Show Gist options
  • Save ryanwood/f0b81213e247f1fe2874 to your computer and use it in GitHub Desktop.
Save ryanwood/f0b81213e247f1fe2874 to your computer and use it in GitHub Desktop.
∴ rc
Loading development environment (Rails 4.2.2)
[1] pry(main)> c = Camera.find 40
Camera Load (0.8ms) SELECT "cameras".* FROM "cameras" WHERE "cameras"."destroyed_at" IS NULL AND "cameras"."id" = $1 LIMIT 1 [["id", 40]]
#<Camera:0x007fe278278d98> {
:id => 40,
:property_id => 6,
:name => "Brooklyn",
:manufacturer => "Browning",
:model => "34",
:lat => 30.3238413410926,
:lng => -81.6751098632812,
:created_at => Sat, 17 Jan 2015 14:34:45 EST -05:00,
:updated_at => Sat, 17 Jan 2015 14:34:45 EST -05:00,
:time_zone_old => nil,
:photos_count => 3,
:destroyed_at => nil
}
[2] pry(main)> c.photos.count
(3.0ms) SELECT COUNT(*) FROM "photos" WHERE "photos"."destroyed_at" IS NULL AND "photos"."camera_id" = $1 [["camera_id", 40]]
3
[3] pry(main)> c.photos.last.destroy
Photo Load (1.1ms) SELECT "photos".* FROM "photos" WHERE "photos"."destroyed_at" IS NULL AND "photos"."camera_id" = $1 ORDER BY "photos"."id" DESC LIMIT 1 [["camera_id", 40]]
SQL (2.6ms) UPDATE "photos" SET "destroyed_at" = '2015-06-19 03:53:46.411615' WHERE "photos"."id" = $1 [["id", 359]]
true
*** Notice it doesn't update the counter_cache ***
[4] pry(main)> reload!
Reloading...
true
[5] pry(main)> c.photos.create!
(0.3ms) BEGIN
SQL (0.5ms) INSERT INTO "photos" ("camera_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["camera_id", 40], ["created_at", "2015-06-19 03:54:59.887523"], ["updated_at", "2015-06-19 03:54:59.887523"]]
SQL (0.4ms) UPDATE "cameras" SET "photos_count" = COALESCE("photos_count", 0) + 1 WHERE "cameras"."id" = $1 [["id", 40]]
(1.1ms) COMMIT
#<Photo:0x007fe27848f488> {
:id => 362,
:camera_id => 40,
:file_name => nil,
:total_file_size => nil,
:taken_at => nil,
:caption => nil,
:notes => nil,
:exif => nil,
:created_at => Thu, 18 Jun 2015 23:54:59 EDT -04:00,
:updated_at => Thu, 18 Jun 2015 23:54:59 EDT -04:00,
:forecast_id => nil,
:upload_key => nil,
:destroyed_at => nil,
:attachments_destroyed_at => nil,
:thumbnailed_at => nil,
:thumbnail_info => nil,
:error => nil,
:migrated => true,
:user_id => nil,
:slug => nil
}
ryanwood@firefly in ~/projects/deerlab/app.deerlab.com on fix-counters!
∴ rc
Loading development environment (Rails 4.2.2)
[1] pry(main)> c = Camera.find 40
Camera Load (0.7ms) SELECT "cameras".* FROM "cameras" WHERE "cameras"."destroyed_at" IS NULL AND "cameras"."id" = $1 LIMIT 1 [["id", 40]]
#<Camera:0x007fe27434eb68> {
:id => 40,
:property_id => 6,
:name => "Brooklyn",
:manufacturer => "Browning",
:model => "34",
:lat => 30.3238413410926,
:lng => -81.6751098632812,
:created_at => Sat, 17 Jan 2015 14:34:45 EST -05:00,
:updated_at => Sat, 17 Jan 2015 14:34:45 EST -05:00,
:time_zone_old => nil,
:photos_count => 4,
:destroyed_at => nil
}
[2] pry(main)> c.photos.count
(0.6ms) SELECT COUNT(*) FROM "photos" WHERE "photos"."camera_id" = $1 [["camera_id", 40]]
4
[3] pry(main)> c.photos.last.destroy
Photo Load (1.1ms) SELECT "photos".* FROM "photos" WHERE "photos"."camera_id" = $1 ORDER BY "photos"."id" DESC LIMIT 1 [["camera_id", 40]]
(0.3ms) BEGIN
SQL (0.4ms) DELETE FROM "photos" WHERE "photos"."id" = $1 [["id", 362]]
SQL (0.3ms) UPDATE "cameras" SET "photos_count" = COALESCE("photos_count", 0) - 1 WHERE "cameras"."id" = $1 [["id", 40]]
(2.0ms) COMMIT
*** Notice it DOES update the counter_cache ***
#<Photo:0x007fe275f07a30> {
:id => 362,
:camera_id => 40,
:file_name => nil,
:total_file_size => nil,
:taken_at => nil,
:caption => nil,
:notes => nil,
:exif => nil,
:created_at => Thu, 18 Jun 2015 23:54:59 EDT -04:00,
:updated_at => Thu, 18 Jun 2015 23:54:59 EDT -04:00,
:forecast_id => nil,
:upload_key => nil,
:destroyed_at => Thu, 18 Jun 2015 23:55:37 EDT -04:00,
:attachments_destroyed_at => nil,
:thumbnailed_at => nil,
:thumbnail_info => nil,
:error => nil,
:migrated => true,
:user_id => nil,
:slug => nil
}
[4] pry(main)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment