alias_attribute and creating and method with the original attribute name causes a loop

Posted by Tiago on Stack Overflow See other posts from Stack Overflow or by Tiago
Published on 2010-05-27T19:17:30Z Indexed on 2010/05/27 19:21 UTC
Read the original article Hit count: 311

Im trying to dynamically create a method chain in one attribute in my model. By now I have this function:

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

so I receive a string with the attribute name, alias it for '*_without_filter*' (alias_method or alias_method_chain fails here, because the attribute isnt there when the class is created), and I create a new method with the attribute name, where I filter its contents.

But somehow, when I call *"#{attribute_name}_without_filter"* it calls my new method (i think because the alias_attribute some how), and the program goes into a stack loop.

Can someone please enlighten me on this.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about alias-method