Ruby-on-rails 如何在Rails中获取属性的原始值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1487548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:49:09  来源:igfitidea点击:

How to get the original value of an attribute in Rails

ruby-on-railsactiverecord

提问by Leonid Shevtsov

is there a way to get the original value that an ActiveRecord attribute (=the value that was loaded from the database)?

有没有办法获取 ActiveRecord 属性的原始值(=从数据库加载的值)?

I want something like this in an observer

我想要一个观察者这样的东西

before_save object
  do_something_with object.original_name
end

The task is to remove the object from a hash table (in fact, move it to another key in the table) upon updating.

任务是在更新时从哈希表中删除对象(实际上,将其移动到表中的另一个键)。

回答by Vincent

Before rails 5.1

在 Rails 5.1 之前

Appending _wasto your attribute will give you the previous value.

附加_was到您的属性将为您提供以前的值。

For rails 5.1+

对于导轨 5.1+

Copied from Lucas Andrade's answer below: https://stackoverflow.com/a/50973808/9359123

复制自 Lucas Andrade 的回答如下https: //stackoverflow.com/a/50973808/9359123



Appending _wasis deprecated in rails 5.1, now you should append _before_last_save

附加_was在 rails 5.1 中已弃用,现在您应该附加_before_last_save

Something like:

就像是:

before_save object
  do_something_with object.name_before_last_save
end

Will return the name value before your last save at database (works for save and create)


The difference between _wasand _before_last_saveaccording to the documentation:

将数据库前,你最后返回名称值保存(适用于保存和创造)


之间的差异_was,并_before_last_save根据文档:

_wassource from docs

_was从文档

def attribute_was(attr)
  attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr)
end

_before_last_savesource from docs

_before_last_save来自文档的源代码

def attribute_before_last_save(attr_name)
  mutations_before_last_save.original_value(attr_name)
end

回答by Lucas Andrade

For rails 5.1+

对于导轨 5.1+

Appending _wasis deprecated in rails 5.1, now you should append _before_last_save

附加_was在 rails 5.1 中已弃用,现在您应该附加_before_last_save

Something like:

就像是:

before_save object
  do_something_with object.name_before_last_save
end

Will return the name value before your last save at database (works for save and create)


The difference between _wasand _before_last_saveaccording to the documentation:

将数据库前,你最后返回名称值保存(适用于保存和创造)


之间的差异_was,并_before_last_save根据文档:

_wassource from docs

_was从文档

def attribute_was(attr)
  attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr)
end

_before_last_savesource from docs

_before_last_save来自文档的源代码

def attribute_before_last_save(attr_name)
  mutations_before_last_save.original_value(attr_name)
end

You can see a better example here

你可以在这里看到一个更好的例子

回答by John Topley

ActiveRecord's attributes_before_type_castmethod returns a hash of attributes before typecasting and deserialization have occurred.

ActiveRecord 的attributes_before_type_cast方法在类型转换和反序列化发生之前返回属性的散列。

回答by rderoldan1

Take a look in rails documentation

查看 Rails 文档

http://api.rubyonrails.org/classes/ActiveModel/Dirty.html

http://api.rubyonrails.org/classes/ActiveModel/Dirty.html

Model.attribute_was return previous value :D

Model.attribute_was 返回以前的值:D