Skip to content

Instantly share code, notes, and snippets.

@addisaden
Last active August 29, 2015 13:57
Show Gist options
  • Save addisaden/9707088 to your computer and use it in GitHub Desktop.
Save addisaden/9707088 to your computer and use it in GitHub Desktop.
How to establish diffrent database-connections
require "active_record"
module Database
def self.config
{
:db1 => { adapter: "sqlite3", database: "db1.db" },
:db2 => { adapter: "sqlite3", database: "db2.db" }
}
end
end
class Post < ActiveRecord::Base
establish_connection Database.config[:db1]
unless connection.table_exists?(table_name) then
connection.create_table table_name, force: true do |t|
t.string :title
t.text :content
end
end
end
class User < ActiveRecord::Base
establish_connection Database.config[:db2]
unless connection.table_exists?(table_name) then
connection.create_table table_name, force: true do |t|
t.string :name
t.timestamp :birthday
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment