我试图在我的模型中的一个属性中动态创建一个方法链。现在我有这个功能:
def create_filtered_attribute(attribute_name)
alias_attribute "#{attribute_name}_without_filter", attribute_name
define_method "#{attribute_name}" do
filter_words(self.send("#{attribute_name}_without_filter"))
end
end
所以我收到一个带有属性名称的字符串,将其别名为' _without_filter '(alias_method 或 alias_method_chain 在这里失败,因为创建类时属性不存在),然后我创建了一个具有属性名称的新方法,我在其中过滤它内容。
但不知何故,当我调用“#{attribute_name}_without_filter”时,它调用了我的新方法(我认为是因为 alias_attribute 某种方式),并且程序进入了堆栈循环。
我试图重命名该属性,因此我可以将其名称用于方法...
有人可以请教我。