MongoDB mongoid 3 应该怎样切换数据库连接?

rubyonme · 2012年06月02日 · 最后由 larryzhao 回复于 2012年06月05日 · 3460 次阅读

mongoid 3 的数据库连接配置都在 mongoid.ymlsessions 下,找了半天也没有找到怎样整体切换 session 的方式。

帮你顶一下。

帮你顶一下。

还没试过

我想通过 2 级域名切换数据库连接,不知道该怎样弄

我自己还没有用过,不过通过最近 mongoid 邮件组里,Durran 回答别人的邮件可以大致看出来应该怎么做:

You need to define a session for each one you want in your mongoid.yml, and then you can tell the models which session to persist to or at runtime you can change it.

The basics are here:

http://mongoid.org/en/mongoid/docs/persistence.html#custom

A session represents your connection to the server (or servers in the case of a replica set). If you want to change the session at runtime you use with #with method:

Model.with(session: "main").create

If you want a model to always persist to a specific session use .store_in:

class Model
  include Mongoid::Document
  store_in session: "secondary"
end

但是根据他后续的邮件,疑似这个还有些问题。你可以注册 mongoid 邮件组来关注进展。

感谢 @larryzhao ,发这个贴的时候,我也在邮件组发了邮件。根据作者提供的方法测试了一下,发现有问题,他还要自己测试一下。

@rubyonme 那看来可以猜出来哪个是你了~

需要 登录 后方可回复, 如果你还没有账号请 注册新账号