Created
January 10, 2019 23:21
-
-
Save jonstefansson/cc9ab75299edbfca2f1e53b81f25e117 to your computer and use it in GitHub Desktop.
A function to get information about an Active Record model in a Rails console session
This file contains 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
# Load this in a Rails console session with: | |
# play ~/Desktop/reflect_on.rb | |
def reflect_on(clazz) | |
result = { | |
column_names: clazz.column_names.sort, | |
associations: clazz.reflect_on_all_associations.map(&:name).sort, | |
class_methods: clazz.methods().find_all do |m_sym| | |
m = clazz.method(m_sym) | |
m.source_location && m.source_location[0].include?('/app/') | |
end.sort, | |
instance_methods: clazz.instance_methods().find_all do |m_sym| | |
m = clazz.instance_method(m_sym) | |
m.source_location && m.source_location[0].include?('/app/') | |
end.sort | |
} | |
ap(result, index: false) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment