Rails STI:如何更改类名和类名之间的映射“类型"列的值 [英] Rails STI: How to change mapping between class name & value of the 'type' column
问题描述
由于公司规定,我不能使用我们的域名类名;我将使用一个类比来代替.我有一个名为 projects 的表,其中有一个名为type"的列,可能的值为indoor"&'户外'.具有室内和室外的记录具有明确的功能分离,并且非常适合作为 STI 实施.不幸的是,我无法更改类型名称,也无法在全局命名空间中添加类.有没有办法为类型"指定不同的值?
Because of company rules I can't use our domain class names; I am going to use an analogy instead. I have a table called projects which has a column called 'type' with possible values as 'indoor' & 'outdoor'. Records having indoor and outdoor have clear functionality separation and would fit pretty neatly as a STI implementation. Unfortunately I can't change the type-names and can't add classes inside the global namespace. Is there a way to specify a different value for 'type'?
注意:我不会尝试为 STI 使用不同的列名而不是类型".除了我的类名之外,我希望有不同的类型值.
Note: I am not trying to use a different column name instead of 'type' for STI. I am looking to have a different value for type, other than my class name.
推荐答案
你可以试试这样的:
class Proyect < ActiveRecord::Base
end
然后是室内班,但有其他名称
Then the Indoor class but with Other name
class Other < Proyect
class << self
def find_sti_class(type_name)
type_name = self.name
super
end
def sti_name
"Indoor"
end
end
end
户外课程同样适用.您可以在 http://apidock.com/rails/ActiveRecord/Base/find_sti_class/中检查 sti_name班级
The same apply for Outdoor class. You can check sti_name in http://apidock.com/rails/ActiveRecord/Base/find_sti_class/class
这篇关于Rails STI:如何更改类名和类名之间的映射“类型"列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!