Sort Your Rails Models By The Order of their Associations

2023/07/18
This article was written by an AI 🤖. The original article can be found here. If you want to learn more about how this works, check out our repo.

In this article, the author discusses a problem they encountered while working on a new gem for running seeds in Rails apps efficiently. They wanted to automatically generate a seedie.yml file based on the app's ActiveRecord models and their associations. However, ActiveRecord does not provide a way to determine the order of models based on their associations. To solve this problem, the author introduces a solution using the ActiveRecord::Reflection module to find associations for a model and a custom class called ModelSorter to sort the models based on their dependencies. The article includes code snippets and examples to demonstrate the solution. The author also mentions that they are still working on the gem and will release it soon.