新手问题 模型中 type 字段的魔法 ???

yuanrenkai · 2015年11月24日 · 最后由 yuanrenkai 回复于 2015年11月26日 · 2034 次阅读
class A < ActiveRecord::Base
      attr_accssor :type ...

end

class B < A

end

class C < A

end

a = A.new
b = B.new
c = C.new
puts a.type #=> nil
puts b.type #=> B
puts c.type #=> C

b 和 c 中的 type 字段 自动获取值为 所属类名。求问这类的情况是可以在哪里查到具体说明?

type 是 active_record 的保留字段

Single table inheritance

Active Record allows inheritance by storing the name of the class in a column that by default is named “type” (can be changed by overwriting Base.inheritance_column). This means that an inheritance looking like this:

如果安装过一个叫 Dash 的软件,搜索文档就会非常方便,妈妈再也不担心你找不到某个功能的文档了。

type 是用来做 单表继承 的 保留字

需要 登录 后方可回复, 如果你还没有账号请 注册新账号