super_diff icon indicating copy to clipboard operation
super_diff copied to clipboard

Support ActiveRecord models with composite primary keys

Open mjobrien opened this issue 8 months ago • 0 comments

When super_diff encounters a model with a composite primary key, it raises an error, undefined method 'to_sym' for an instance of Array.

Preferably it would correctly support composite primary keys, but not erroring would be an improvement.

Poking around in the code, it appears that building on #237 and #282 would be a reasonable approach. A majority of the work might be in setting up the testing environment. I tweaked attributes_for_super_diff locally and was able to eliminate the error I saw with the implementation below. This change does not break any existing tests. Similar methodology can likely be used elsewhere.

class ActiveRecord::Base
  # TODO: Remove this monkey patch if possible
  def attributes_for_super_diff
    id_attrs = Array.wrap(self.class.primary_key)

    (attributes.keys.sort - id_attrs).reduce(
      attributes.slice(*id_attrs).symbolize_keys
    ) { |hash, key| hash.merge(key.to_sym => attributes[key]) }
  end
end

mjobrien avatar Aug 04 '25 16:08 mjobrien