Last active
August 29, 2015 13:57
-
-
Save addisaden/9707088 to your computer and use it in GitHub Desktop.
How to establish diffrent database-connections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.db |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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