Skip to content

Instantly share code, notes, and snippets.

@MolarFox
Created May 19, 2025 03:17
Show Gist options
  • Select an option

  • Save MolarFox/122e4edc56d73670cad70a2ba7915f66 to your computer and use it in GitHub Desktop.

Select an option

Save MolarFox/122e4edc56d73670cad70a2ba7915f66 to your computer and use it in GitHub Desktop.
Ruby / common attributes in array of models
def common_attributes(records)
# records should be T::Array[M] of any model M
common = records.first.attributes.dup
records.each do |r|
common.each_key do |key|
common.delete(key) if r.attributes[key] != common[key]
end
end
common # Hash of all attributes that had the same value in all rows in array
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment