Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Multi-Tenancy with Rails & Postgres

jurre
September 24, 2014

Multi-Tenancy with Rails & Postgres

Short talk I did at the Groningen.rb meetup about multi tenant rails apps.

Some links to stuff I mentioned:
Postgres Schema's: http://www.postgresql.org/docs/9.3/static/ddl-schemas.html
Apartment gem: https://github.com/influitive/apartment
Example app: https://github.com/DefactoSoftware/Hours

jurre

September 24, 2014
Tweet

More Decks by jurre

Other Decks in Technology

Transcript

  1. “Multi-tenancy refers to a principle in software architecture where a

    single instance of the software runs on a server, serving multiple client- organizations (tenants)” - Wikipedia
  2. # app/models/user.rb class User < ActiveRecord::Base belongs_to :account end !

    # app/models/category.rb class Category < ActiveRecord::Base belongs_to :account end ! # app/models/account.rb class Account < ActiveRecord::Base has_many :users has_many :categories end ! # app/controllers/categories_controller.rb class CategoriesController < ApplicationController def index @categories = current_account.categories end ! private def current_account current_user.account end end
  3. # app/models/user.rb class User < ActiveRecord::Base end ! # app/models/category.rb

    class Category < ActiveRecord::Base end ! # app/models/account.rb class Account < ActiveRecord::Base end ! # app/controllers/categories_controller.rb class CategoriesController < ApplicationController def index @categories = Category.all end end
  4. database (hours_production) ! accounts ! id subdomain created_at updated_at !

    ! ! users ! id name encypted_password account_id created_at updated_at ! ! categories ! id name created_at updated_at ! ! entries ! id hours user_id category_id created_at updated_at ! ! schema (defacto) ! users ! id name encypted_password account_id created_at updated_at ! ! categories ! id name created_at updated_at ! ! entries ! id hours user_id category_id created_at updated_at ! ! schema (grunnrb) !