DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

@kon_yu
@kon_yu

Posted on

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".

Top comments (0)

🌚 Life is too short to browse without dark mode