# Is a DB connection via an options hash supported?

**URL:** <https://discourse.rom-rb.org/t/is-a-db-connection-via-an-options-hash-supported/153>\
**Category:** Issues\
**Created:** [April 14, 2017, 10:33pm UTC](https://discourse.rom-rb.org/t/is-a-db-connection-via-an-options-hash-supported/153 "2017-04-14T22:33:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mezterious](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@mezterious](https://discourse.rom-rb.org/u/mezterious)\
**Post date:** [April 14, 2017, 10:33pm UTC](https://discourse.rom-rb.org/t/is-a-db-connection-via-an-options-hash-supported/153/1 "2017-04-14T22:33:40Z")

</div>

I’m not sure where I’m supposed to be asking this. If this is the wrong place, can you please redirect me to the correct spot and I can re-post there.

I only discovered rom rb a couple of months ago. I’m also a bit of a newbie when it comes to ruby. I’m trying to connect to the database using an options hash rather than a connection string, like so:

`@rom = ROM.container(:sql, adapter: 'mysql2', user: 'someuser', password: 'somepassword', database: 'somedb', host: '127.0.0.1', port: 6603) do |config| ... end`

I’m not using a framework or anything. I’m just doing everything in a standalone directory, trying to learn how this all works. I’ve used the sequel gem before and was able to connect using an options hash and assumed that we could do the same thing via rom but I get the following error:

`URI::InvalidURIError: bad URI(is not URI?): {:adapter=>"mysql2", :user=>"someuser", :password=>"somepassword", :database=>"somedb", :host=>"127.0.0.1", :port=>6603}`

In trying to learn how the connection is supposed to work, I traced it down to the file `rom-sql-1.2.2/lib/rom/sql/gateway.rb`

`def connect(uri, *args) case uri when ::Sequel::Database uri else ::Sequel.connect(uri.to_s, *args) end end`

Is the `::Sequel.connect` line supposed to convert the uri.to\_s? If I remove the to\_s and let it go into into the sequel connect as a hash, things just seem to work.

Am I misusing rom rb if I want to be able to connect via an options hash?  
The documentation only provides using a connection string as an example so I’m wondering whether I’m doing something incorrectly.

Any help would be much appreciated.

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/solnic/32/279_2.png) [@solnic](https://discourse.rom-rb.org/u/solnic)\
**Post date:** [April 15, 2017, 11:07am UTC](https://discourse.rom-rb.org/t/is-a-db-connection-via-an-options-hash-supported/153/2 "2017-04-15T11:07:09Z")

</div>

At the moment URIs are required, and it’s going to stay like that. Hashes are more complex to handle and in most cases people use ENV to store connection URIs anyway.

---

<div class="post-metadata">

**Author:** ![flash-gordon](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/flash-gordon/32/186_2.png) [@flash-gordon](https://discourse.rom-rb.org/u/flash-gordon)\
**Post date:** [April 16, 2017, 12:51pm UTC](https://discourse.rom-rb.org/t/is-a-db-connection-via-an-options-hash-supported/153/3 "2017-04-16T12:51:02Z")

</div>

ROM can accept a Sequel connection instance for such cases.

---

<div class="post-metadata">

**Author:** ![cflipse](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/cflipse/32/23_2.png) [@cflipse](https://discourse.rom-rb.org/u/cflipse)\
**Post date:** [April 18, 2017, 2:33pm UTC](https://discourse.rom-rb.org/t/is-a-db-connection-via-an-options-hash-supported/153/4 "2017-04-18T14:33:02Z")

</div>

For an illustrative example, look at [https://github.com/rom-rb/rom-rails/blob/master/lib/rom/rails/active\_record/configuration.rb](https://github.com/rom-rb/rom-rails/blob/master/lib/rom/rails/active_record/configuration.rb)

This is the code that `ROM::Rails` uses to extract and use the hash-configuration it extracts from activerecord, in a case where we’ve got a mixed adapter setup in a Rails project.

---

<div class="post-metadata">

**Author:** ![mezterious](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@mezterious](https://discourse.rom-rb.org/u/mezterious)\
**Post date:** [May 1, 2017, 9:40am UTC](https://discourse.rom-rb.org/t/is-a-db-connection-via-an-options-hash-supported/153/5 "2017-05-01T09:40:38Z")

</div>

Thank you for the help and code examples. It is much appreciated and a good learning experience for me. I’ve since found out that the reason I was getting a bad URI was that the password contained an invalid character so it failed the URI parse.
