DEV Community

@kon_yu
@kon_yu

Posted on

1 2

Explore files under a specific directory in Rails

If you want to recursively retrieve the file paths under a particular directory, you can use the
Use the Dir class to get the path.

If you want to call it in Rails, you can use Rails.root to get the absolute path where Rails is running on the OS you're running, so you don't have to lead the production or development environment.

execution example

Dir.glob("#{Rails.root}/app/models/**/*") do |f
  p f
end

=> "/var/app/models/user.rb"
"/var/app/models/owner.rb"
"/var/app/models/blog".
"/var/app/models/blogs/comment.rb".
Enter fullscreen mode Exit fullscreen mode

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