I am trying to figure out how to retrieve data from a database without having to place a connection string at the top of each ruby ββfile.
I learn the basics of ruby ββthrough the small Sinatra application that I compile, which retrieves data from an MSSQL database.
So far, I have managed to create various simple erb pages that display data from the MSSQL database using the following code structure at the top of each file: -
<% client = TinyTds::Client.new(:username => 'name', :password => 'password', :dataserver => 'hostname', :database => 'database') %> <% data = client.execute("SELECT * from tablename") %>
From the books, tutorials and online tutorials that I found based on many configurations for PostgreSQL or MySQL databases, it seems to me that I need to create a central file to store the connection data (for example, database. Yml), and then referring to that somewhere / somehow in my application.
Will this be correct, and should I do this in my main.rb file so that each of my .erb files does not require a connection string or do I still need to reference the database in each .erb file
I noticed links to creating database configuration variables, such as: -
db_config = YAML.load(File.Open("/path_to_file/database.yml")) [ENV['RAILS_ENV']]
but this is clearly similar to Rails applications.
Can I do something similar for my Sinatra app?
Thanks.
lad33 source share