How to access multiple databases with rails 1 comment
I know it exists other information about this, but I want to summarize here the practise I use in several project now. It takes 4 steps:
- configure your database.yml
- create an abstract class for connection
- create new models within a module
- interact the 2 databases
1. Configure your database.yml
This is the same as you create production or development connection, just give it a specific name:
other_database_connection:
adapter: postgresql
encoding: utf8
database: database_name
username: user
password: password
host: localhost2. Create an abstract class for connection
Then create an abstract class you will inheritate by your new classes:
class External < ActiveRecord::Base
self.abstract_class = true
establish_connection :other_database_connection
end3. Create new models within a module
To make it easier to use and cleaner, create a subfolder OtherDatabase and then create in this folder your new classes (for each table you need) as following:
class OtherDatabase::NewClass1 < External
end4. How to interact the 2 databases
Once you have created your new classes, you can simply access it using the module prefix:
new_object = OtherDatabase::NewClass1.new
.
result = OtherDatabase::NewClass1.find(:all)
Conclusion
That’s all, submodule is not really necessary but keeps your model folder cleaner. Any suggestion to improve this practise ?
Rails: find controller for a path no comments
Huh, seems simple to do :p, but I’ve been seeking for a while to find the answer and dig into rails code to find a simple method to retrieve the controller of a given path (could be useful to build a navigation). There it is:
ActionController::Routing::Routes.recognize_path(path)[:controller]Ebb, even faster than Thin no comments
Ebb is new server to put in front of a rackable ruby application.
Have a look to the Ebb website to get some comparison with other app servers.
sudo gem install ebbWhat I needed to do before on Mac OS X (leopard with MacPorts):
sudo port install glib2Git: Easy way to setup a private remote repository 9 comments
Git is different from svn and create a private repository on a remote server too. No need of gitosis or git-daemon for this.
I start from an existing project and a server I already access to with ssh key.
First, initialize git in your project:
git init
git add .
git commit -m 'initial import'[update]
Then create a git copy of your project into a git archive my_project_folder.git.
git clone—bare my_project_folder my_project_folder.git
Copy this archive the .git folder to your remote server:
scp -rp .git user@server://path/to/repositories/my_project_folder.gitAdd this new repository in the config of your local project:
git remote add my_remote_repo ssh://server/path/to/repositories/my_project_folder.gitYou’re done. After a change you can commit and then push your change to the remote repo:
git commit -m "message for change log"
git push my_remote_repoUse your own parameters for:
- my_project_folder with the folder of your project
- path/to/repositories with the path to your repository on the remote server
- my_project_repo with the name you want for the remote repository, usually origin
If you need a public repository, have look to gitosis or git-daemon. Best is Github.




