0

我想在更新時添加after_commit以更新我的模型,在該模型中,每次我的模型對象從我的應用程序的任何位置獲得更新時,都會將更新的字段值發送到我的應用程序的另一部分。瞭解哪些字段是從ActiveRecord更新

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 

如何只獲取更新的字段,所以我可以推他們?

回答

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 
相關問題