Rails 继承 Model 的类不能重新定义 initialize 方法吗?

zouyu · May 28, 2018 · Last by cysh replied at May 28, 2018 · 1625 hits

Category 是一个 model

class C1 < Category
   def initialize
   end
end
C1.new报如下错误

initialize 是特殊方法,需要加 super

zouyu closed this topic. 28 May 11:18
zouyu reopened this topic. 28 May 11:18

查了一下 ActiveRecord 不支持在 model 中 override initialize 方法。有大佬知道详细信息吗?

https://ruby-china.org/topics/23523

不要 override initialize 方法,很多 ar 的对象不是 new 出来的。如果非要有什么想在 initialize 的时候做的事情,可以使用 after_initialize

....我是直接重新定义了 self.new 方法,这样做 create 也会调用它

You need to Sign in before reply, if you don't have an account, please Sign up first.