开发者

Multi-tenant applications in Rails 3 with DB separation

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 ca开发者_如何学运维n 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.


To switch between databases, this solution looks cleaner. Update the conn method will the logic of your get_db_name method. Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜