Skip to content

Support ActiveRecord models with composite primary keys #291

@mjobrien

Description

@mjobrien

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions