为什么压倒一切的ActiveRecord :: Base.initialize错了吗? [英] Why is overriding ActiveRecord::Base.initialize wrong?
问题描述
在一些地方,我已经看到了重写的ActiveRecord ::索赔是Base.initialize错误的,因为它可能并不总是被称为:
In several places, I've seen claims that overriding ActiveRecord::Base.initialize is wrong because it might not always be called:
-
<一个href="http://stackoverflow.com/questions/328525/what-is-the-best-way-to-set-default-values-in-activerecord">What是在ActiveRecord的设置默认值的最佳方式是什么?
的http://blog.dalethatcher.com/2008/03/rails-dont-override-initialize-on.html
使用从未版本的ActiveRecord(3.0+)的,这仍然是真的吗?如果是这样,有什么具体是什么情况下它时,人们可能会想到,这将是不叫?
With never versions of ActiveRecord (3.0+), is this still true? If so, what specifically are the circumstances under which it is not called when one might expect that it would be?
推荐答案
这并不是说就不会被调用,它的存在已经的是的的初始化
在的ActiveRecord :: Base的
。
It's not that it won't be called, it's that there already is an initialize
, in ActiveRecord::Base
.
能的你叫超
?也许,也许,在一般。在整个Rails的版本,包括未来的呢?再次回复朦胧的尝试。不能predict现在。后来再次询问。魔术8球是不服气。
Could you call super
? Probably, maybe, in general. Across Rails versions, including future ones? Reply hazy try again. Cannot predict now. Ask again later. Magic 8-Ball isn't convinced.
有一个初始化的回调,所以你并不需要,并且不动心。
There's an initialize callback so you don't need to, and aren't tempted.
这篇关于为什么压倒一切的ActiveRecord :: Base.initialize错了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!