了解哪些字段从 ActiveRecord 更新

Knowing which fields are updated from ActiveRecord

我想添加一个 after_commit on update 到我的模型中,每当我的模型对象从我的应用程序中的任何位置获得更新时,我都会将更新的字段值发送到我的应用程序的另一部分。

class Product
  after_commit :push_to_socket, on: :update

  def push_to_socket
     # Push the updated fields values to socket
     # fields = {name: 'value', ...}
     # push(fields)
  end
end

如何只获取更新的字段以便推送它们?

更新的字段:

class Product
  after_update :push_to_socket

  def push_to_socket
     # Push the updated fields values to socket
     # previous_changes captures the changes that were made
     # I don't know how your push method works, but this will slice out the changed attributes
     push(slice(*previous_changes.keys))
  end
end