Skip to content

This is a working Rails 7 sample from GoRails multi-tenancy video using the apartment rubygem

Notifications You must be signed in to change notification settings

robault/MultiTenancy

Repository files navigation

Commands used:

rails new MultiTenancy -d=postgresql
bundle exec rails generate scaffold Home tagline:string
bundle exec rails generate scaffold Message text:string
bundle exec rails generate scaffold Tenant subdomain:string
bundle exec rake db:drop db:create db:migrate

Additions made:

layouts/_header.html.erb

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <div class="container-fluid">
    <a class="navbar-brand" href="<%= homes_path %>">Home</a>
    <a class="navbar-brand" href="<%= messages_path %>">Messages</a>
  </div>
</nav>

layouts/application.html.erb

  <body>
    <%= render "layouts/header" %>
    <%= yield %>
  </body>

config/routes.rb

root "homes#index"

config/environments/development.rb

config.hosts << "lvh.me"

Addition to gem file:

gem 'ros-apartment', require: 'apartment' # works with rails 7
bundle exec rails generate apartment:install

In a rails console:

Apartment::Tenant.create('glitzy')
bundle exec rails generate scaffold User email tenant:references

With apartment setup, there is no need for "references" to a tenant. This next scaffold automatically works because it is not in the exclusion list in the apartment.rb initializer.

bundle exec rails generate scaffold Projects title

From the GoRails video:

https://gorails.com/episodes/multitenancy-with-apartment?autoplay=1

Database Issue

Each PostgreSQL schema has the same tables as any other. Only by looking at the apartment initalizer exclusions can anyone know which tables are tenant specific.

Ideally there would be a single "shared" schema and a schema for each tenant. This would allow the database to be more easily understood by a database administrator.

About

This is a working Rails 7 sample from GoRails multi-tenancy video using the apartment rubygem

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published