我想在我的模型的更新中添加after_commit
,每当我的模型对象从我的应用程序中的任何地方获得更新时,我都会将更新的字段值发送到我的应用程序的另一部分。
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
如何只获取更新后的字段,以便推送它们?
答案 0 :(得分:3)
更新的字段:
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