Since your question was quite broad, I'll have to answer accordingly.
--
What you're asking is something called "multi tenancy":
Software Multitenancy refers to a software architecture in which a single instance of a software runs on a server and serves multiple tenants. A tenant is a group of users who share a common access with specific privileges to the software instance.
This is definitely the realm of development teams; you need several components to get it working, which Rails is not really equipped for.
Having said that, there is a popular way of achieving it with Apartment
& PGSQL schemas.
--
Real multi-tenancy should have separate computing configurations, with their own resources and data-pools; Rails can only run on one server and - without massive hacking - one database.
If you wanted to create a system which handles doctors individually, you'll want to look at scoping your data. This is what PGSQL schemas do:
#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
The above gives you subdomains (the most base level of scoping), which will allow you to use the following:
#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
The above will allow you to access http://1.doctor.com/patients
to see all the patients for that doctor:
#app/views/patients/index.html.erb
<% @patients.each do |patient| %>
<%= patient.name %>
<% end %>
--
Of course, the above is a rudimentary example, with neither the database or application level security in place to maintain data integrity.
The main challenge of multi tenancy (with Rails) is creating as water-tight system as possible.