Created
May 19, 2025 03:17
-
-
Save MolarFox/122e4edc56d73670cad70a2ba7915f66 to your computer and use it in GitHub Desktop.
Ruby / common attributes in array of models
This file contains hidden or 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
| 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