Ruby on Rails
Wednesday, December 21, 2016
Hi.
Do someone knows about specification for `after_create` ?
For the following implementations, the `after_create` method will be called after "INSERT SQLs".
```
class Article < ActiveRecord::Base
has_many :comments
after_create :observe_after_create
def observe_after_create
binding.pry
end
end
```
```
Loading development environment (Rails 4.2.7.1)
irb(main):001:0> article = Article.new
=> #<Article id: nil, title: nil, created_at: nil, updated_at: nil>
irb(main):002:0> article.comments.build
=> #<Comment id: nil, article_id: nil, comment: nil, created_at: nil, updated_at: nil>
irb(main):003:0> article.save
(0.1ms) begin transaction
SQL (0.9ms) INSERT INTO "articles" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2016-12-22 06:20:58.522888"], ["updated_at", "2016-12-22 06:20:58.522888"]]
SQL (0.3ms) INSERT INTO "comments" ("article_id", "created_at", "updated_at") VALUES (?, ?, ?) [["article_id", 6], ["created_at", "2016-12-22 06:20:58.527376"], ["updated_at", "2016-12-22 06:20:58.527376"]]
From: /Users/work/test_app/app/models/article.rb @ line 7 Article#observe_after_create:
6: def observe_after_create
=> 7: binding.pry
8: end
[1] pry(#<Article>)> exit
(0.8ms) commit transaction
=> true
irb(main):004:0>
```
I think it was correct.
Also, I tried same using `ActiveSupport::Concern.included` block.
For the following implementations, the `after_create` method will be called **between** "INSERT SQLs".
```
module Observable
extend ActiveSupport::Concern
included do
after_create :observe_after_create
end
end
class Article < ActiveRecord::Base
include Observable
has_many :comments
def observe_after_create
binding.pry
end
end
```
```
Loading development environment (Rails 4.2.7.1)
irb(main):002:0> article = Article.new
=> #<Article id: nil, title: nil, created_at: nil, updated_at: nil>
irb(main):003:0> article.comments.build
=> #<Comment id: nil, article_id: nil, comment: nil, created_at: nil, updated_at: nil>
irb(main):004:0> article.save
(0.1ms) begin transaction
SQL (1.0ms) INSERT INTO "articles" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2016-12-22 06:29:19.820454"], ["updated_at", "2016-12-22 06:29:19.820454"]]
From: /Users/work/test_app/app/models/article.rb @ line 7 Article#observe_after_create:
6: def observe_after_create
=> 7: binding.pry
8: end
[1] pry(#<Article>)> exit
SQL (0.2ms) INSERT INTO "comments" ("article_id", "created_at", "updated_at") VALUES (?, ?, ?) [["article_id", 8], ["created_at", "2016-12-22 06:29:27.678304"], ["updated_at", "2016-12-22 06:29:27.678304"]]
(3.3ms) commit transaction
=> true
```
Is it correct specification ? Or, Is it bug ?
My environment was below.
- Mac OS X 10.11.6
- Ruby 2.3.3
- Rails 4.2.7.1
Thanks
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/7c59a795-e6c5-4002-9acc-931425f53079%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment