dynamic database connections
class ApplicationController < ActionController::Base
private
def message_databaser
Message.establish_connection(
:host => current_user.database.host,
:username => current_user.database.username,
:password => current_user.database.password,
:database => current_user.database.database
)
end
def friend_databaser
Message.establish_connection(
:host => current_user.database.host,
:username => current_user.database.username,
:password => current_user.database.password,
:database => current_user.database.database
)
end
end
class MessagesController < ApplicationController
before_filter :message_databaser
def index
@messages = Message.all
end
end
class FriendsController < ApplicationController
before_filter :friend_databaser
def index
@friends = Friends.all
end
end
Some models uses other databases which depends on the current user. Now i want to clear this code little bit. i want to write a method like that:
def databaser(model开发者_运维知识库_name, user)
model_name.establish_connection(
:host => user.database.host,
:username => user.database.username,
:password => user.database.password,
:database => user.database.database
)
end
and i want to write a query like that:
Message.databaser(Message, current_user).all
But i couldnt write. Any help will be appreciated.
精彩评论