Created
November 3, 2022 05:54
-
-
Save baweaver/c076216d740e310fd05213b042c4b693 to your computer and use it in GitHub Desktop.
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
module AccessedAttributes | |
def self.included(klass) | |
association_proxy = Module.new do | |
def accessed_associations | |
@_accessed_associations ||= [] | |
end | |
end | |
klass.reflect_on_all_associations.each do |association| | |
name = association.name | |
association_proxy.define_method(name) do |*args, &block| | |
accessed_associations << name | |
super(*args, &block) | |
end | |
end | |
klass.prepend(association_proxy) | |
end | |
end | |
class MyModel < ApplicationRecord | |
include AccessedAttributes | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Josh wins: https://twitter.com/josh_cheek/status/1588046756171890690