【发布时间】:2010-05-27 19:17:30
【问题描述】:
我试图在我的模型中的一个属性中动态创建一个方法链。 现在我有了这个功能:
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 某种方式),然后程序进入堆栈循环。
我正在尝试重命名该属性,因此我可以将其名称用于方法...
有人可以请教我。
【问题讨论】:
标签: ruby-on-rails alias-method alias-method-chain