所以我在数据库中强制执行相当严格的字符串布局,但我不想让程序员坚持这一点。所以我想告诉程序员"传递给我什么,我会把它放在数据库所要求的格式中。"
对于更新和新命令,我需要ActiveRecord中的以下重载:
def user_type=(ut)
super(ut.to_s.camelize.singularize.to_sym)
end
def action=(ut)
super(ut.to_s.underscore.singularize.to_sym)
end
def model=(ut)
super(ut.to_s.underscore.singularize.to_sym)
end
这些值会在设置时清理它们,因此它可以保证数据库始终是正确的格式。
如果我想将其扩展到所有内容,例如
,该怎么办?find_or_create_by(user_type: user_type, action: action, model: model)
因为没有设置值,所以它们会避免我的卫生方法。
除了覆盖类中的每个方法之外,是否有一种机制来处理它?</ p>