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 method
read' 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:-
- I need to read & modify the database configuration before passing it to my middleware as argument.
- 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!