目前在我的 Rails 应用程序中,我有几个不同产品的类。
例如,一个例子是Circuits
.
我想要做的是创建一个名为的新类Service
,并让所有单个产品模型都继承自它。
以前我的circuit.rb
模型是
class Circuit < ActiveRecord::Base
但现在是
class Circuit < Service
我创建了一个新的 `Services1 类,简单地说:
class Service < ActiveRecord::Base
end
在我的circuit_controller.rb
我有一些功能,最直接的是list
def list
conditions = []
conditions = ["organisation_id = ?", params[:id]] if params[:id]
@circuits = Circuit.paginate(:all, :page => params[:page], :conditions => conditions, :per_page => 40)
end
但是将circuit
模型更改为从services
导致我的电路列表视图为空,这是我没想到的。
在我的services
表格中,我包含了一个type
用于存储产品类型的字段,但目前表格为空。
多表继承是最好的方法吗?该应用程序非常大,因此我不想重构大量代码来实现此更改。
单表继承肯定是不行的,所以我想知道某种关联是否会更好。
更新
刚刚尝试关注此博客文章:
http://rhnh.net/2010/08/15/class-table-inheritance-and-eager-loading
所以我添加了
belongs_to :service
到我的个人产品模型,然后在service
s 模型中
SUBCLASSES = [:circuit, :domain]
SUBCLASSES.each do |class_name|
has_one class_name
end
end
然后在service_controller.rb
def list
@services = Service.all(:include => Service::SUBCLASSES)
end
最后,在列表视图中,我尝试检查和调试@services
变量,但它是空的,因为查询在空services
表上运行,它不应该也在circuits
anddomains
表上运行吗?