Rails - 在before_save中获取旧值

时间:2022-12-22 01:20:41

I'm trying to get the old value in the before_save by adding "_was" to my value but it doesn't seem to work.

我试图通过在我的值中添加“_was”来获取before_save中的旧值,但它似乎不起作用。

Here is my code:

这是我的代码:

before_save :get_old_title

def get_old_title
    puts "old value #{self.title_was} =>  #{self.title}"
  end

Both "title_was" and "title" got the new title just been saved.

“title_was”和“title”都获得了新标题。

Is it possible to get the old value inside before_save ?

是否可以在before_save中获取旧值?

3 个解决方案

#1


8  

Instead of before_save use before_update. It should work now.

而不是before_save使用before_update。它现在应该工作。

#2


3  

The reason for you getting the same value is most probably because you check the output when creating the record. The callback before_save is called on both create() and update() but on create() both title and title_was are assigned the same, initial value. So the answer is "yes, you can get the old value inside before_save" but you have to remember that it will be different than the current value only if the record was actually changed. This means that the results you are getting are correct, because the change in question doesn't happen when the record is created.

获得相同值的原因很可能是因为您在创建记录时检查输出。在create()和update()上调用回调before_save,但在create()上,title和title_was都被赋予相同的初始值。所以答案是“是的,你可以在before_save中得到旧值”,但你必须记住,只有当记录实际改变时,它才会与当前值不同。这意味着您获得的结果是正确的,因为创建记录时不会发生问题更改。

#3


0  

So, the answer above might work but what if I wanted to get the previous value for some reason and use it to perform some task then you would need to get the previous value. In my case I used this

所以,上面的答案可能会有效,但如果我想出于某种原因获得之前的值并使用它来执行某些任务,那么你需要获得之前的值。就我而言,我用过这个

after_update do
  if self.quantity_changed?
    sku.decrement(:remaining, (self.quantity_was - self.quantity) * sku.quantity)
  end
end

The _was and _changed? added to any of the columns would do the job to get the job done.

_was和_changed?添加到任何列都可以完成工作。

#1


8  

Instead of before_save use before_update. It should work now.

而不是before_save使用before_update。它现在应该工作。

#2


3  

The reason for you getting the same value is most probably because you check the output when creating the record. The callback before_save is called on both create() and update() but on create() both title and title_was are assigned the same, initial value. So the answer is "yes, you can get the old value inside before_save" but you have to remember that it will be different than the current value only if the record was actually changed. This means that the results you are getting are correct, because the change in question doesn't happen when the record is created.

获得相同值的原因很可能是因为您在创建记录时检查输出。在create()和update()上调用回调before_save,但在create()上,title和title_was都被赋予相同的初始值。所以答案是“是的,你可以在before_save中得到旧值”,但你必须记住,只有当记录实际改变时,它才会与当前值不同。这意味着您获得的结果是正确的,因为创建记录时不会发生问题更改。

#3


0  

So, the answer above might work but what if I wanted to get the previous value for some reason and use it to perform some task then you would need to get the previous value. In my case I used this

所以,上面的答案可能会有效,但如果我想出于某种原因获得之前的值并使用它来执行某些任务,那么你需要获得之前的值。就我而言,我用过这个

after_update do
  if self.quantity_changed?
    sku.decrement(:remaining, (self.quantity_was - self.quantity) * sku.quantity)
  end
end

The _was and _changed? added to any of the columns would do the job to get the job done.

_was和_changed?添加到任何列都可以完成工作。