Created
April 27, 2013 02:33
-
-
Save seapy/5471661 to your computer and use it in GitHub Desktop.
for Rails ActiveRecord Callbacks order check
This file contains 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 Post < ActiveRecord::Base | |
belongs_to :user | |
attr_accessible :body, :title | |
after_destroy :after_destroy | |
private | |
def after_destroy | |
puts "Post after_destroy" | |
end | |
end |
This file contains 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 User < ActiveRecord::Base | |
has_many :posts, :dependent => :destroy | |
attr_accessible :email, :name | |
before_validation :before_validation | |
after_validation :after_validation | |
before_save :before_save | |
around_save :around_save | |
after_save :after_save | |
before_create :before_create | |
around_create :around_create | |
after_create :after_create | |
before_update :before_update | |
around_update :around_update | |
after_update :after_update | |
before_destroy :before_destroy | |
around_destroy :around_destroy | |
after_destroy :after_destroy | |
private | |
def method_name | |
caller.first[/`.*'/][1..-2] | |
end | |
def print_method | |
puts caller.first[/`.*'/][1..-2] | |
end | |
def before_validation | |
print_method | |
# false | |
end | |
def after_validation | |
print_method | |
# raise 'RORLab Exception' | |
end | |
def before_save | |
print_method | |
end | |
def around_save | |
puts "start #{method_name}" | |
yield | |
puts "end #{method_name}" | |
end | |
def after_save | |
print_method | |
end | |
def before_create | |
print_method | |
end | |
def around_create | |
puts "start #{method_name}" | |
yield | |
puts "end #{method_name}" | |
end | |
def after_create | |
print_method | |
end | |
def before_update | |
print_method | |
end | |
def around_update | |
puts "start #{method_name}" | |
yield | |
puts "end #{method_name}" | |
end | |
def after_update | |
print_method | |
end | |
def before_destroy | |
print_method | |
end | |
def around_destroy | |
puts "start #{method_name}" | |
yield | |
puts "end #{method_name}" | |
end | |
def after_destroy | |
print_method | |
end | |
end |
This file contains 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
irb(main):002:0> u = User.new | |
=> #<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil> | |
irb(main):003:0> u.save | |
(0.1ms) begin transaction | |
before_validation | |
after_validation | |
before_save | |
start around_save | |
before_create | |
start around_create | |
SQL (0.6ms) INSERT INTO "users" ("created_at", "email", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Sat, 27 Apr 2013 02:27:38 UTC +00:00], ["email", nil], ["name", nil], ["updated_at", Sat, 27 Apr 2013 02:27:38 UTC +00:00]] | |
end around_create | |
after_create | |
end around_save | |
after_save | |
(2.6ms) commit transaction | |
=> true | |
irb(main):004:0> u.save | |
(0.1ms) begin transaction | |
before_validation | |
after_validation | |
before_save | |
start around_save | |
before_update | |
start around_update | |
end around_update | |
after_update | |
end around_save | |
after_save | |
(0.1ms) commit transaction | |
=> true | |
irb(main):005:0> |
This file contains 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
irb(main):006:0> u = User.first | |
User Load (0.3ms) SELECT "users".* FROM "users" LIMIT 1 | |
=> #<User id: 5, name: nil, email: nil, created_at: "2013-04-26 16:17:07", updated_at: "2013-04-26 16:17:07"> | |
irb(main):007:0> u.posts << Post.create | |
(0.0ms) begin transaction | |
SQL (1.5ms) INSERT INTO "posts" ("body", "created_at", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?) [["body", nil], ["created_at", Sat, 27 Apr 2013 02:32:05 UTC +00:00], ["title", nil], ["updated_at", Sat, 27 Apr 2013 02:32:05 UTC +00:00], ["user_id", nil]] | |
(1.1ms) commit transaction | |
(0.1ms) begin transaction | |
(0.4ms) UPDATE "posts" SET "user_id" = 5, "updated_at" = '2013-04-27 02:32:05.122799' WHERE "posts"."id" = 4 | |
(0.7ms) commit transaction | |
Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = 5 | |
=> [#<Post id: 4, title: nil, body: nil, user_id: 5, created_at: "2013-04-27 02:32:05", updated_at: "2013-04-27 02:32:05">] | |
irb(main):008:0> u.destroy | |
(0.1ms) begin transaction | |
SQL (0.5ms) DELETE FROM "posts" WHERE "posts"."id" = ? [["id", 4]] | |
Post after_destroy | |
before_destroy | |
start around_destroy | |
SQL (0.2ms) DELETE FROM "users" WHERE "users"."id" = ? [["id", 5]] | |
end around_destroy | |
after_destroy | |
(2.6ms) commit transaction | |
=> #<User id: 5, name: nil, email: nil, created_at: "2013-04-26 16:17:07", updated_at: "2013-04-26 16:17:07"> | |
irb(main):009:0> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment