Skip to content

Instantly share code, notes, and snippets.

@zsarge
Last active August 7, 2023 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zsarge/95ed1805efb52077126453d3362f3d44 to your computer and use it in GitHub Desktop.
Save zsarge/95ed1805efb52077126453d3362f3d44 to your computer and use it in GitHub Desktop.
A quick hacky function to see the classes necessary to create an ActiveRecord object.
# A quick hacky function to see the other ActiveRecord objects necessary to create an ActiveRecord object.
# There might be a better way to do this natively.
# these are both one line so they can be quickly found with Ctrl+R
def _needs(obj) = (if obj.nil? then return obj end; props = obj.class.columns.map(&:name).filter{_1.ends_with?("_id") && _1 != "reference_id"}.map{_1.gsub('_id', '')}; if props.nil? || props.size == 0 then return obj else return [obj, props.map{_needs(obj.send(_1.to_sym))}.reject{_1.nil?}].compact end)
def needs(obj) = _needs(obj).flatten.map(&:class).map(&:name).uniq
# Usage:
needs User.last # => ["User", "AccountType", "WhateverOtherClass", ...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment