DEV Community

@kon_yu
@kon_yu

Posted on

3 1

Get the list of classes connected to the DB

It was quite troublesome before Rails4, but after Rails5
You can get a list of models connected to DB in ApplicationRecord.descendants.

For example, something like this.

model_list = ApplicationRecord.descendants

# If you want to output a list of class names
model_list.map { |v| v.to_s }
=> ["User", "Owner", "Blogs::Comment"]
Enter fullscreen mode Exit fullscreen mode

Usage scenarios

  • Batch processing
  • If you are looking for a method that can be executed by the method xxx

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more