作者tonytonyjan (南洋大兜虫)
看板Ruby
标题[问题] after_initialize and after_find
时间Thu Aug 25 12:22:04 2011
环境:
Ruby 1.9.2 Rails 3
状况:
我在阅读Rails Guide时,发现 after_initialize 和 after_find
没有相对的 before_initialize 及 before_find
并说要使用时必须以 method define,不能用 handler
摘录:
If you try to register after_initialize or after_find using macro-style class
methods, they will just be ignored.
This behaviour is due to performance reasons, since after_initialize and
after_find will both be called for each record found in the database,
significantly slowing down the queries.
我觉得这样的解释我还是看不太懂
1. 这和 self reflection 有关系吗?
2. 一个 select query 可能包涵数万笔资料,不管是用 method define 还是 handler
,都要被呼叫数万次吧?如此效能还不是一样差吗?
3. 对一个 callback 同时使用 method define 和 handler 会导致 handler 被盖掉吗?
4. 我想看原始码,但是在github里面有如大海捞针,希望得到众神的指引
附录:
http://goo.gl/17Zmh
章节10.4
---
1F:→ ayamomiji:我想是因为这两个事件发生时, 还没有对应的 instance 08/25 13:26
愿闻其详QQ
※ 编辑: tonytonyjan 来自: 61.227.147.76 (08/25 19:09)