Skip to content

Instantly share code, notes, and snippets.

@briandunn
Last active October 25, 2018 19:00
Show Gist options
  • Save briandunn/90b9d2c79a2fba1fa6643cb252d01014 to your computer and use it in GitHub Desktop.
Save briandunn/90b9d2c79a2fba1fa6643cb252d01014 to your computer and use it in GitHub Desktop.
list tables and columns of non-polymorphic foreign keys
ApplicationRecord.descendants.flat_map do |c|
c.reflect_on_all_associations.select do |a|
ActiveRecord::Reflection::BelongsToReflection === a && !a.options[:polymorphic]
end
end.map do |r|
[r.active_record.table_name, r.plural_name, r.options.slice(:optional, :foreign_key)]
end.uniq.sort
@briandunn
Copy link
Author

Also used the optional option to determine if the fk column should be not null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment