Rails 对象的属性名称是变量,怎样处理?

zhang_xiangyu · May 10, 2012 · Last by hooopo replied at May 10, 2012 · 2120 hits

object.attribute attribute 可能是 name 也可能是 value。

object.send :name
object.send :value

已经查到了,object.send(attribute.to_sym)

obj.name obj.value ???

额 明白了

#2 楼 @zhang_xiangyu send 不必须传符号的,字符串就可以:

object.send(attribute)

这个应该叫动态方法调用~

You need to Sign in before reply, if you don't have an account, please Sign up first.