继承了rails模型,如何写入不同的表?
How to write into different tables when rails model has been inherited?
我有以下型号:
class Building < ActiveRecord::Base;end
class Department < Building;end
class Organization < Building;end
当我创建部门和组织并获取所有部门时
2.1.5 :008 > Department.all.count
(0.5ms) SELECT COUNT(*) FROM "buildings"
=> 2
我想在这种情况下得到
2.1.5 :008 > Department.all.count
(0.5ms) SELECT COUNT(*) FROM "departments"
=> 1
2.1.5 :008 > Organization.all.count
(0.5ms) SELECT COUNT(*) FROM "organizations"
=> 1
2.1.5 :008 > Buildings.all.count
(0.5ms) SELECT COUNT(*) FROM "buildings"
=> 2
我该怎么做?
使用性病。有一个名为 buildings 的 table 和一个名为 'type' 的列。让新的 classes 扩展建筑 class。每个 class 都可以有选择地拥有自己不同的关系。您将能够实例化Building并获得类型定义的class或直接实例化所需的class。
我有以下型号:
class Building < ActiveRecord::Base;end
class Department < Building;end
class Organization < Building;end
当我创建部门和组织并获取所有部门时
2.1.5 :008 > Department.all.count
(0.5ms) SELECT COUNT(*) FROM "buildings"
=> 2
我想在这种情况下得到
2.1.5 :008 > Department.all.count
(0.5ms) SELECT COUNT(*) FROM "departments"
=> 1
2.1.5 :008 > Organization.all.count
(0.5ms) SELECT COUNT(*) FROM "organizations"
=> 1
2.1.5 :008 > Buildings.all.count
(0.5ms) SELECT COUNT(*) FROM "buildings"
=> 2
我该怎么做?
使用性病。有一个名为 buildings 的 table 和一个名为 'type' 的列。让新的 classes 扩展建筑 class。每个 class 都可以有选择地拥有自己不同的关系。您将能够实例化Building并获得类型定义的class或直接实例化所需的class。