0
嘗試從保存的對象獲取以前的值。想想這個場景的:如何在保存時獲取更改的值
@object = {:name => 'Dan', :occupation => 'student'} @object[:occupation] = 'Full time employee' @object.value_was[:occupation] # => 'student'
我希望這是足夠的理解,有沒有方法value_was
。更多我想在模型對象上做同樣的事情:
@student = Student.find(1) @student.occupation = 'Full time employee' @student.save @student.value_was(:occupation) # => 'student'
任何幫助將不勝感激。
那將是很有益
很好的答案!你也知道如何得到改變的對象,那真是太棒了!例如新對象:'@student#=> {:name =>'Dan',:職業=>'全職員工',並通過調用我在髒字段中找不到的方法get - ' @ student.was#=> {:name =>'Dan',:職業=>'學生'}' –
你會想要'@ student.previous_changes',它應該返回一個由保存更改的列表。 –
返回空hash {} –