0

I am creating a Rack Middleware which I want to use in my Rails App. Basically, I need to log requests matching particular urls to my Database. In order to do this I need to pass database configuration to my Middleware so it can establish connection with DB. I am trying to do:-

db_yml = Rails.root.join('config/database.yml')
db_config = YAML.load(db_yml.read)[Rails.env]

But this is giving an error

config/application.rb:40:in <class:Application>': undefined methodread' for # (NoMethodError)

If I add byebug and run the same in the byebug console it works fine. I am not able to find out why. I want to do following things:-

  1. I need to read & modify the database configuration before passing it to my middleware as argument.
  2. I want to read the domain of the request url. We are using Apartment gem and Our schema name will be the domain name.

I followed multiple articles here & here. I am a newbie to Rails and don't know good resources so please help. Thanks in advance!

abhinav
  • 607
  • 6
  • 18

1 Answers1

1

You should use:

db_yml = Rails.root.join('config/database.yml')
db_config = YAML.load(File.open(db_yml))[Rails.env]

Rails.root.join('config/database.yml') return the file path which is a string.

jack-nie
  • 736
  • 6
  • 21
  • But why does my snippet work in byebug console? And how can I access the request URL in application.rb? – abhinav Sep 04 '16 at 08:30