Created
September 5, 2017 10:36
-
-
Save mvaragnat/90de7abb2b9f1f352b6d0819c5283dae to your computer and use it in GitHub Desktop.
Rails : How to compare (diff) to ActiveRecord models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Hash.diff is deprecated in Rails 5 | |
# this method does not handle deep hashes (it could be made by recursion though) | |
class MyModel < ApplicationRecord | |
def diff(contract) | |
differences = {} | |
self.attributes.each do |k,v| | |
if contract[k] != v | |
differences[k] = {v => contract[k]} | |
end | |
end | |
return differences | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment