Ruby: Dynamically calling available methods raising undefined method (metaprogramming)

Posted by user94154 on Stack Overflow See other posts from Stack Overflow or by user94154
Published on 2010-04-21T23:29:48Z Indexed on 2010/04/21 23:33 UTC
Read the original article Hit count: 256

I have an Activerecord object called Foo:

Foo.attribute_names.each do |attribute|
  puts Foo.find(:all)[0].method(attribute.to_sym).call
end

Here I'm calling all attributes on this model (ie, querying for each column value). However, sometimes, I'll get an undefined method error.

How can ActiveRecord::Base#attribute_names return an attribute name that when converted into its own method call, raises an undefined method error?

Thank

© Stack Overflow or respective owner

Related posts about ruby

Related posts about rails