一个rails项目连多个mongo数据库

 

一个rails项目如何连多个mongo数据库呢,答案在这里

http://stackoverflow.com/questions/16400458/connecting-to-two-databases-mongoid

 

(1)先在config/mongoid.yml配置一下

development:
  sessions:
    default:
      database: ott_tv_cms_development
      hosts:
        - localhost:27017
      options:
    live:
      database: ott_tv_live_cms_development
      hosts:
        - localhost:27017
      options:

  options:

这是development的环境,其他的也一样呢,在session下面新增个数据库链接就行

 

(2)在对应的model里增加链接配置信息

就是那句store_in 

class LiveChannel
  include Mongoid::Document
  include Timestamps

  store_in session: "live", collection: 'live_channel'

  field :logo, type: String
  field :name, type: String
  field :channel, type: String

  has_many :live_videos, primary_key: 'channel', foreign_key: 'channel'
end

搞定啦~

 

posted @ 2015-09-10 16:41  冰凌花花~  阅读(173)  评论(0编辑  收藏  举报