bee*_*lez 11 ruby activerecord ruby-on-rails activemodel
在控制台中使用自动完成功能时,我经常看到" _was"后缀为我的属性.但我找不到任何文档或最佳实践用法.它做了什么以及如何使用它?
示例:user.fname具有该方法user.fname_was
使用source_location,我已将其跟踪到:active_model/attribute_methods.rb",第296行,但没有任何具体内容.
Fiv*_*ell 37
这是ActiveModel :: Dirty的一部分你可以在这里看到它https://github.com/rails/rails/blob/af64ac4e5ce8406137d5520fa88e8f652ab703e9/activemodel/lib/active_model/dirty.rb#L146 示例
person = Person.find_by_name('Uncle Bob')
person.changed? # => false
Run Code Online (Sandbox Code Playgroud)
更改名称:
person.name = 'Bob'
person.changed? # => true
person.name_changed? # => true
#method _was return prev attribute value
person.name_was # => 'Uncle Bob'
person.name_change # => ['Uncle Bob', 'Bob']
person.name = 'Bill'
person.name_change # => ['Uncle Bob', 'Bill']
Run Code Online (Sandbox Code Playgroud)