Last active
December 22, 2015 06:08
-
-
Save workmaster2n/6428542 to your computer and use it in GitHub Desktop.
after_comitt not firing?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1.9.3-p392 :001 > Zone.find(455).touch | |
| (12.3ms) SELECT * FROM geometry_columns WHERE f_table_name='zones' | |
| Zone Load (32.6ms) SELECT "zones".* FROM "zones" WHERE "zones"."id" = $1 LIMIT 1 [["id", 455]] | |
| SQL (0.9ms) UPDATE "zones" SET "updated_at" = '2013-09-03 19:44:19.881546' WHERE "zones"."id" = 455 | |
| => true | |
| //notice that the string "hey there, I'm cleared" is not printed | |
| 1.9.3-p392 :002 > Zone.cached_find 455 | |
| Zone Load (0.4ms) SELECT "zones".* FROM "zones" WHERE "zones"."id" = $1 LIMIT 1 [["id", 455]] | |
| => #<Zone id: 455, location_id: 15, default_state: nil, created_at: "2013-08-06 18:28:01", updated_at: "2013-09-03 19:44:19", name: "Tyler's Office", rotation: #<BigDecimal:7fca2ebb1340,'0.9E2',9(18)>, collada: "wilcox_building", cpx: #<BigDecimal:7fca2ebb0aa8,'0.0',9(18)>, cpy: #<BigDecimal:7fca2ebb07b0,'0.0',9(18)>, cpz: #<BigDecimal:7fca2ebb06e8,'0.0',9(18)>, group_id: 6> | |
| 1.9.3-p392 :003 > Zone.find(455).touch | |
| Zone Load (0.4ms) SELECT "zones".* FROM "zones" WHERE "zones"."id" = $1 LIMIT 1 [["id", 455]] | |
| SQL (0.7ms) UPDATE "zones" SET "updated_at" = '2013-09-03 19:45:11.039368' WHERE "zones"."id" = 455 | |
| => true | |
| 1.9.3-p392 :004 > Zone.cached_find 455 | |
| => #<Zone id: 455, location_id: 15, default_state: nil, created_at: "2013-08-06 18:28:01", updated_at: "2013-09-03 19:44:19", name: "Tyler's Office", rotation: #<BigDecimal:7fca2e826b30,'0.9E2',9(18)>, collada: "wilcox_building", cpx: #<BigDecimal:7fca2e8269f0,'0.0',9(18)>, cpy: #<BigDecimal:7fca2e826900,'0.0',9(18)>, cpz: #<BigDecimal:7fca2e826810,'0.0',9(18)>, group_id: 6> | |
| //notice that the cached_find is not using the correct zone (updated_at is the original updated_at instead of the one based off of the touch) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Zone < ActiveRecord::Base | |
| after_commit :flush_cache | |
| def self.cached_find(id) | |
| Rails.cache.fetch([name, id]) { find(id) } | |
| end | |
| private | |
| def flush_cache | |
| puts "hey there, I'm cleared" | |
| Rails.cache.delete([self.class.name, id]) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment