Skip to content

Instantly share code, notes, and snippets.

@mvaragnat
Created September 5, 2017 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvaragnat/90de7abb2b9f1f352b6d0819c5283dae to your computer and use it in GitHub Desktop.
Save mvaragnat/90de7abb2b9f1f352b6d0819c5283dae to your computer and use it in GitHub Desktop.
Rails : How to compare (diff) to ActiveRecord models
# 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