What is the ActiveModel method attribute “_was” used for?

前端 未结 1 631
情话喂你
情话喂你 2020-12-31 10:02

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

1条回答
  •  时光说笑
    2020-12-31 10:50

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

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

    Change the name:

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

    0 讨论(0)
提交回复
热议问题