order 表里面有个 type 字段默认值 OrderType
Order.first.class
OrderType
取出来的类型是 OrderType 有没有什么方法能让取出来的类型还是 Order
去看一下 becomes 和 becomes! 方法
class AAddress < Address
end
class BAddress < Address
end
a_address = Address.new(type: 'AAddress')
puts a_address.class.name
结果 'AAddress'
puts a_address.becomes(Address).class.name
结果 'Address'
type
是 ActiveRecord 的单表继承默认关键字,可以改成别的,这样 type
字段就可以用了
class Order < ActiveRecord::Base
self.inheritance_column = "_type"
end