百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rails_multisite

> 编程语言
开源

Rails 应用程序的多租户支持

269 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

Rails 应用程序的多租户支持

Rails Multisite

This gem provides multi-db support for Rails applications.

Using its middleware you can partition your app so each hostname has its own db.

It provides a series of helper for working with multiple database, and some additional rails tasks for working with them.

It was extracted from Discourse. https://discourse.org

Installation

Add this line to your application's Gemfile:

gem 'rails_multisite'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rails_multisite

Usage

Configuration requires a file called: config/multisite.yml that specifies connection specs for all dbs.

mlp:
  adapter: postgresql
  database: discourse_mlp
  username: discourse_mlp
  password: applejack
  host: dbhost
  pool: 5
  timeout: 5000
  host_names:
    - discourse.equestria.com
    - discourse.equestria.internal

drwho:
  adapter: postgresql
  database: discourse_who
  username: discourse_who
  password: "Up the time stream without a TARDIS"
  host: dbhost
  pool: 5
  timeout: 5000
  host_names:
    - discuss.tardis.gallifrey

Execute a query on each connection

RailsMultisite::ConnectionManagement.each_connection do |db|
  # run query in context of db
  # eg: User.find(1)
end
RailsMultisite::ConnectionManagement.each_connection(threads: 5) do |db|
  # run query in context of db, will do so in a thread pool of 5 threads
  # if any query fails an exception will be raised
  # eg: User.find(1)
end

Usage with Rails

RAILS_DB Environment Variable

When working with a Rails application, you can specify the DB that you'll like to work with by specifying the RAILS_DB ENV variable.

# config/multisite.yml

db_one:
  adapter: ...
  database: some_database_1

db_two:
  adapater: ...
  database: some_database_2

To get a Rails console that is connected to some_database_1 database:

RAILS_DB=db_one rails console

CDN origin support

To avoid needing to configure many origins you can consider using RailsMultisite::ConnectionManagement.asset_hostnames

When configured, requests to asset_hostname?__ws=another.host.name will be re-routed to the correct site. Cookies will be stripped on all incoming requests.

Example:

  • Multisite serves sub.example.com and assets.example.com
  • RailsMultisite::ConnectionManagement.asset_hostnames = ['assets.example.com']
  • Requests to https://assets.example.com/route/?__ws=sub.example.com will be routed to the sub.example.com

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyrubygem

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言