Created
August 9, 2015 20:05
-
-
Save rilian/3850498b0206a23d5eeb to your computer and use it in GitHub Desktop.
testing AR rollback inside methods
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
[6] pry(main)> def test_inner | |
[6] pry(main)* puts 'i0' | |
[6] pry(main)* raise ActiveRecord::Rollback | |
[6] pry(main)* puts 'i1' | |
[6] pry(main)* return false | |
[6] pry(main)* puts 'i2' | |
[6] pry(main)* end | |
=> :test_inner | |
[7] pry(main)> | |
[8] pry(main)> def test_outer | |
[8] pry(main)* puts 'o0' | |
[8] pry(main)* ActiveRecord::Base.transaction do | |
[8] pry(main)* puts 'o1' | |
[8] pry(main)* User.create(email:'[email protected]') | |
[8] pry(main)* puts 'o2' | |
[8] pry(main)* test_inner | |
[8] pry(main)* puts 'o3' | |
[8] pry(main)* return false | |
[8] pry(main)* puts 'o4' | |
[8] pry(main)* end | |
[8] pry(main)* puts 'o5' | |
[8] pry(main)* end | |
=> :test_outer | |
[9] pry(main)> xx = test_outer | |
o0 | |
(0.2ms) BEGIN | |
o1 | |
User Exists (0.4ms) SELECT 1 AS one FROM "users" WHERE ("users"."email" = '[email protected]') LIMIT 1 | |
SQL (1.6ms) INSERT INTO "users" ("email", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["email", "[email protected]"], ["created_at", "2015-08-09 19:59:07.625768"], ["updated_at", "2015-08-09 19:59:07.625768"]] | |
o2 | |
i0 | |
(0.2ms) ROLLBACK | |
o5 | |
=> nil | |
[10] pry(main)> User.count | |
(0.5ms) SELECT COUNT(*) FROM "users" | |
=> 0 | |
[11] pry(main)> xx | |
=> nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
result: rollback works; methods return
nil