Skip to content

Instantly share code, notes, and snippets.

@basiszwo
Created September 4, 2013 09:51
Show Gist options
  • Save basiszwo/6434984 to your computer and use it in GitHub Desktop.
Save basiszwo/6434984 to your computer and use it in GitHub Desktop.
Rails and MSSQL Server

Rails and MSSQL

Install freeTDS (http://freetds.schemamania.org/)

brew install freetds for OSX Checkout what's required for Ubuntu.

Install tiny_tds for using freetds on

Check working connection

Check https://github.com/rails-sqlserver/tiny_tds for using / testing the connection.

Gemfile

gem 'tiny_tds'

Ruby code to test the db connection

client = TinyTds::Client.new(:username => 'user', :password => 'password', :host => '10.0.0.15', :database => 'DBNAME')
client.active?

result = client.execute("SELECT * FROM anfrage_details")

Connecting to multiple databases

Gemfile

gem 'activerecord-sqlserver-adapter'

config/databse.yml

megasuche_zdb:
  adapter: sqlserver
  encoding: dblib
  host: 10.0.0.15
  # port: 2301 # ???
  database: DBNAME
  username: user
  password: password

development:
  adapter: postgresql
  encoding: unicode
  database: megasearch_development
  pool: 5
  username: sb
  password:

Ruby model that connects the MSSQL Server

class CAS::AnfragenDetails < ActiveRecord::Base
  establish_connection "megasuche_zdb"
end

Links

Youtube Video: http://youtu.be/iJvIt3_bQY8

multi connections in Rails: https://github.com/cherring/connection_ninja

http://stackoverflow.com/questions/1825844/multiple-databases-in-rails http://stackoverflow.com/questions/1226182/how-do-i-work-with-two-different-databases-in-rails-with-active-records http://www.quarkruby.com/2010/5/24/moving-from-mysql-to-mssql-server-in-rubyonrails

ActiveRecord SQL Adapter

https://github.com/rails-sqlserver/activerecord-sqlserver-adapter

Database config: https://github.com/arthrex/activerecord-sqlserver-adapter/blob/master/test/config.yml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment