由于你的问题很广泛,我必须相应地回答。
--
你要问的是一个叫做"multi tenancy"的东西:
软件多租户是指一种软件架构,其中软件的单个实例在服务器上运行并为多个租户提供服务。租户是一组用户,他们共享对软件实例具有特定权限的公共访问权限。
这绝对是开发团队的领域;你需要几个组件才能让它工作,Rails 并没有真正配备。
话虽如此,有一种流行的方法可以通过Apartment 和PGSQL schemas 实现它。
--
真正的多租户应该有独立的计算配置,有自己的资源和数据池; Rails 只能在一台服务器上运行 - 无需大规模黑客攻击 - 一个数据库。
如果您想创建一个单独处理医生的系统,您需要查看范围您的数据。这就是 PGSQL 模式的作用:
#config/routes.rb
scope constraints: SubDomain do
resources :patients
end
#lib/sub_domain.rb
module SubDomain
def initializer(router)
@router = router
end
def self.matches?(request)
Doctor.exists? request.subdomain
end
end
以上为您提供subdomains(范围界定的最基本级别),这将允许您使用以下内容:
#app/models/doctor.rb
class Doctor < ActiveRecord::Base
has_many :patients
end
#app/models/patient.rb
class Patient < ActiveRecord::Base
belongs_to :doctor
end
#app/controllers/patients_controller.rb
class PatientsController < ApplicationController
before_action :set_doctor
def index
@patients = @doctor.patients
end
def show
@patient = @doctor.patients.find params[:id]
end
private
def set_doctor
@doctor = Doctor.find request.subdomain
end
end
以上将允许您访问http://1.doctor.com/patients 以查看该医生的所有患者:
#app/views/patients/index.html.erb
<% @patients.each do |patient| %>
<%= patient.name %>
<% end %>
--
当然,以上只是一个初步的例子,没有数据库或应用程序级别的安全性来维护data integrity。
多租户(使用 Rails)的主要挑战是创建尽可能防水的系统。