本文介绍了什么是加载ActiveModel方法属性和QUOT; _was"用于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在控制台中使用自动完成,我经常看到 _was postpended我的属性。但是,我找不到任何使用或文件的最佳实践。它是干什么的以及应当如何使用?

When using autocomplete in the console, I often see "_was" postpended to my attributes. But I can't find any documentation or best practices for usage. What does it do and how should it be used?

例如: user.fname 的方法 user.fname_was

使用源位置,我已经跟踪它到:active_model / attribute_methods.rb,线路296,但没有任何具体的

Using source_location, I've tracked it down to: active_model/attribute_methods.rb", line 296 but there isn't anything specific.

推荐答案

这是加载ActiveModel的一部分::脏在这里你可以看到它https://github.com/rails/rails/blob/af64ac4e5ce8406137d5520fa88e8f652ab703e9/activemodel/lib/active_model/dirty.rb#L146示例

That is a part of ActiveModel::DirtyYou can see it here https://github.com/rails/rails/blob/af64ac4e5ce8406137d5520fa88e8f652ab703e9/activemodel/lib/active_model/dirty.rb#L146Example

person = Person.find_by_name('Uncle Bob')
person.changed?       # => false

更改名称:

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']

这篇关于什么是加载ActiveModel方法属性和QUOT; _was"用于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 15:51