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
某些模型使用依赖于当前用户的其他数据库。现在我想稍微清除这段代码。我想写一个像这样的方法:
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
我想编写一个类似的查询:
Message.databaser(Message, current_user).all
但我不能写。任何帮助将不胜感激。