Last active
August 7, 2023 16:51
-
-
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.
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
# 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