我正在尝试清理以字符串形式出现的属性。我已经设置了一个模块,其中包含模型将调用的多种方法validate
。
班级:
class Preset < ActiveRecord::Base
include Shared::AttributeDataSanitizer
class AttributeError < StandardError ; end
validate :attribute_sanitization
def attribute_sanitization
raise AttributeError if sanitize_attributes(string).errors.length > 0
rescue AttributeError
errors.add(:string, "errored, some of your attributes are just terrible.")
false
end
end
模块:
module Shared::AttributeDataSanitizer
extend ActiveSupport::Concern
def sanitize_attributes(string)
errors = []
text = string.dup
extract(text).each do |attr|
if attr.validation != "valid"
errors << result
end
end
def extract(text)
calculation.scan(/(?<=\[).*?(?=\])/)
end
end
end
当在extract
内部调用sanitize_attributes
时会发生以下错误:
[Rollbar] Reporting exception: undefined method `extract' for #<Preset:0x007f7dce91ba60>
[Rollbar] 未报告异常,因为 Rollbar 已禁用
NoMethodError(未定义的方法extract' for #<Preset:0x007f7dce91ba60>):
app/models/concerns/shared/attribute_sanitizer.rb:8:in
sanitize_data_attributes' app/models/preset.rb:27:in attribute_sanitization'
app/controllers/presets_controller.rb:25:in
block in update' app/controllers/presets_controller.rb:24:in `update'
extract
不是私有方法,但这是否会引发错误,因为它认为它是类方法?这应该是一个实例方法吗?注意:不止一个类需要使用它。