Skip to content

Instantly share code, notes, and snippets.

@jonstefansson
Created January 10, 2019 23:21
Show Gist options
  • Save jonstefansson/cc9ab75299edbfca2f1e53b81f25e117 to your computer and use it in GitHub Desktop.
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
# 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