mwp mwp - 1 year ago 89
Ruby Question

Use DATABASE_URL by default with Sinatra/Sequel extension

The README for sinatra/sequel states that the

set :database
statement can be omitted and it will use the
environment variable by default. Looking at the source, this appears to be true. However, omitting this line causes the following error when it tries to load my model definitions:

in `db': No database associated with Sequel::Model: have you called Sequel.connect
or Sequel::Model.db= ? (Sequel::Error)

I realize I'm being a bit pedantic by wanting to omit this line (and not have to write
:set database, ENV['DATABASE_URL']
) but for the sake of learning is there a way to invoke the default behavior of this module? I asked the question over here as well. Thanks in advance.

mwp mwp
Answer Source

As I answered in the linked GitHub issue, the best way to do it is to simply call database from the configure block, and this will invoke the default logic.

configure do
  abort 'Unable to establish database connection' unless database
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download