0
我的单表继承系统根据我的服务中的输入字符串创建它的子类。我想知道是否有更好的方式来编写这个代码,因为它看起来很笨重。有没有更好的方法来创建Rails子类?
客户不应该知道,也不应该关心我的子分类结构是什么。当收到请求时,我得到的参数是:
{calculator: {course: 'science'}}
类:
class Calculator < ActiveRecord::Base
before_save :subclass, on: :create
def subclass
case course.downcase
when "science"
self.type = "Calculator::ScientificCalc"
when "standard"
self.type = "Calculator::StandardCalc"
end
end
end
看来臭有对象上的回调,然后设置“self.type”。任何机构都有更好的解决方案?
类型列已在表格中。在这种情况下,我不希望我的客户将参数提交为“课程”名称以外的任何内容。 – brettu