Rails 3 中具有数据库分离的多租户应用程序
如何正确更改 Rails 3 中多租户应用程序的数据库配置?
此时,我正在 ApplicationController 的 before 过滤器中切换数据库配置,如以下代码
class ApplicationController < ActionController::Base
before_filter :set_database
def set_database
db_name = get_db_name
spec = ActiveRecord::Base.configurations[Rails.env]
new_spec = spec.clone
new_spec["database"] = db_name
ActiveRecord::Base.establish_connection(new_spec)
end
end
这是一个好方法吗?我对用户会话有疑问。如何正确更改会话存储设置,例如 :key
?这里的另一个问题是,如果用户会话存储在数据库中,因为用户会话似乎是在 ApplicationController 代码之前加载到机架中间件中的。
How to correctly change database configuration for multi-tenant app in Rails 3?
At this point, I'm switching DB configuration in ApplicationController's before filter, like following code
class ApplicationController < ActionController::Base
before_filter :set_database
def set_database
db_name = get_db_name
spec = ActiveRecord::Base.configurations[Rails.env]
new_spec = spec.clone
new_spec["database"] = db_name
ActiveRecord::Base.establish_connection(new_spec)
end
end
Is this a good way? I have a concerns regarding user sessions. How can I correctly change session store settings, e.g. :key
? Another problem here is, if user session is stored in DB, because it seems that user session is loaded in rack middleware before ApplicationController code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要在数据库之间切换,这个解决方案看起来更干净。更新
conn
方法将是get_db_name
方法的逻辑。希望这有帮助。To switch between databases, this solution looks cleaner. Update the
conn
method will the logic of yourget_db_name
method. Hope this helps.