0

In my config/database.yml file i need to retrieve the current file path. I've tried this :

<%
  logger = Logger.new(STDOUT)
  logger.debug __FILE__
%>

That outputs (erb) instead of ./config/database.yml

I know i can get the file path using

Rails.root.join('config', 'database.yml')

But that's just not satisfying as it's not dynamic.

The goal here is to get the unix name of the file owner in order to use it as a prefix to the database name so more than one developer can work with different database without the risk of committing unwanted changes :

development:
    database: <%= Etc.getpwuid(File.stat(__FILE__).uid).name %>_project_db
Crystark
  • 3,693
  • 5
  • 40
  • 61

1 Answers1

1

In case anyone finds this, here's how i did it in the end:

In application.rb

class Application < Rails::Application
  config.whoami = Etc.getpwuid(File.stat(__FILE__).uid).try(:name)
  config.whoami.try(:downcase!)

  [...]
end

And in database.yaml

development:
  database: <%= Rails.application.config.whoami %>_project_db
Crystark
  • 3,693
  • 5
  • 40
  • 61