所以我試圖在屬性上使用_was方法。 可以說我有一個對象Person
:在rails中使用_was方法
Person.name = "new name"
Person.name_was = "old name"
如果我是叫Person.changed?
它會返回["name"]
。
所以我的問題是:我可以以某種方式使用該值從person.changed
做這樣的事情:
Person.changed.each do |x|
Person.x => returns undefined value for x
Person['x'] => returns new name
Person["#{'x'}_was"] => errors
end
有沒有一種方法,我可以使用_was
方法與x
字符串變量?
這不會工作:'person.method(attr)',因爲它只會返回一個代表方法的對象。爲了實際_invoke_方法,你需要發送'call'消息:'p person.method(attr).call'。 – tompave
正確,謝謝 – jonathanccalixto