我有四个表:jp_properties、cn_properties、jp_dictionaries 和 cn_dictioanries。
并且每个 jp_property 都属于表中具有外键“dictionary_id”的 jp_dictionary。
同样,每个 cn_property 也属于表中具有外键“dictionary_id”的 cn_dictionary。
由于属性模型和字典模型中都有很多相同的函数,我想将所有这些函数分组到 abstract_class 中。
模型是这样的:
class Property < AR::Base
self.abstract_class = true
belongs_to :dictionry,
:foreign_key=>'dictionary_id',
:class=> ---ModelDomainName--- + "Dictionary"
### functions shared by JpProperty and CnProperty
end
class Dictionary < AR::Base
self.abstract_class = true
has_many :properties,
:foreign_key=>'dictionary_id',
:class=> ---ModelDomainName--- + "Dictionary"
### functions shared by JpDictionary and CnDictionary
end
class JpProperty < Property
:set_table_name :jp_properties
end
class CnProperty < Property
:set_table_name :cn_properties
end
class JpDictionary < Dictionary
:set_table_name :jp_dictionaries
end
class CnDictionary < Dictionary
:set_table_name :cn_dictionaries
end
从上面的代码可以看出,---ModelDomainName--- 部分是“Jp”或“Cn”。我想从 JpProperty、JpDictionary、CnProperty 或 CnDictionary 的实例中动态获取这些字符串。
例如:
tempJpProperty = JpProperty.first
tempJpProperty.dictionary #=> will get 'Jp' from tempJpProperty's class name and then apply it to the "belongs_to" declaration.
所以问题是我不知道如何指定 ---ModelDomainName--- 部分。
更具体地说,我不知道如何在父类的主体中获取子类的实例对象的类名。
你能帮我解决这个问题吗?
编辑:
有人有什么想法吗?
基本上,我想问的是如何“延迟创建关联,直到创建子类。”?