导轨,使用脏或改变?用after_commit标记

时间:2022-01-13 01:20:05

I heard rails has a dirty/change flag. Is it possible to use that in the after_commit callback?

我听说rails有脏/改变标志。是否可以在after_commit回调中使用它?

In my user model I have:

在我的用户模型中,我有:

after_commit :push_changes

In def push_changes I would like a way to know if the name field changed. Is that possible?

在def push_changes中,我想知道名称字段是否更改。那可能吗?

2 个解决方案

#1


10  

You can do a few things to check...

你可以做几件事来检查......

First and foremost, you can check an individual attribute as such:

首先,您可以检查单个属性:

user = User.find(1)
user.name_changed? # => false
user.name = "Bob"
user.name_changed? # => true

But, you can also check which attributes have changed in the entire model:

但是,您还可以检查整个模型中哪些属性已更改:

user = User.find(1)
user.changed     # => []
user.name = "Bob"
user.age = 42
user.changed     # => ['name', 'age']

There's a few more things you can do too - check out http://api.rubyonrails.org/classes/ActiveModel/Dirty.html for details.

还有一些你可以做的事情 - 详情请查看http://api.rubyonrails.org/classes/ActiveModel/Dirty.html。

Edit:

But, given that this is happening in an after_commit callback, the model has already been saved, meaning knowledge of the changes that occurred before the save are lost. You could try using the before_save callback to pick out the changes yourself, store them somewhere, then access them again when using after_commit.

但是,鉴于这是在after_commit回调中发生的,模型已经被保存,这意味着在保存之前发生的更改的知识将丢失。您可以尝试使用before_save回调自己选择更改,将它们存储在某处,然后在使用after_commit时再次访问它们。

#2


22  

You can use previous_changes in after_commit to access a model's attribute values from before it was saved.

您可以使用after_commit中的previous_changes来访问保存之前的模型属性值。

see this post for more info: after_commit for an attribute

有关更多信息,请参阅此帖子:属性的after_commit

#1


10  

You can do a few things to check...

你可以做几件事来检查......

First and foremost, you can check an individual attribute as such:

首先,您可以检查单个属性:

user = User.find(1)
user.name_changed? # => false
user.name = "Bob"
user.name_changed? # => true

But, you can also check which attributes have changed in the entire model:

但是,您还可以检查整个模型中哪些属性已更改:

user = User.find(1)
user.changed     # => []
user.name = "Bob"
user.age = 42
user.changed     # => ['name', 'age']

There's a few more things you can do too - check out http://api.rubyonrails.org/classes/ActiveModel/Dirty.html for details.

还有一些你可以做的事情 - 详情请查看http://api.rubyonrails.org/classes/ActiveModel/Dirty.html。

Edit:

But, given that this is happening in an after_commit callback, the model has already been saved, meaning knowledge of the changes that occurred before the save are lost. You could try using the before_save callback to pick out the changes yourself, store them somewhere, then access them again when using after_commit.

但是,鉴于这是在after_commit回调中发生的,模型已经被保存,这意味着在保存之前发生的更改的知识将丢失。您可以尝试使用before_save回调自己选择更改,将它们存储在某处,然后在使用after_commit时再次访问它们。

#2


22  

You can use previous_changes in after_commit to access a model's attribute values from before it was saved.

您可以使用after_commit中的previous_changes来访问保存之前的模型属性值。

see this post for more info: after_commit for an attribute

有关更多信息,请参阅此帖子:属性的after_commit