Passing multiple arguments to Rack middleware in Rails application.rb
<p>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:-</p>

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

<p>But this is giving an error</p>

<blockquote>
  <p>config/application.rb:40:in <code>&lt;class:Application&gt;': undefined method</code>read' for # (NoMethodError)</p>
</blockquote>

<p>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:-</p>

<ol>
<li>I need to read &amp; modify the database configuration before passing it to my middleware as argument.</li>
<li>I want to read the domain of the request url. We are using <a href="https://github.com/influitive/apartment" rel="nofollow">Apartment</a> gem and Our schema name will be the domain name.</li>
</ol>

<p>I followed multiple articles <a href="http://deliv.github.io/2014/01/31/rack-middleware-to-log-api-requests-in-mongodb/" rel="nofollow">here</a> &amp; <a href="http://ieftimov.com/writing-rails-middleware" rel="nofollow">here</a>.
I am a newbie to Rails and don't know good resources so please help. 
Thanks in advance!</p>