10

I'm developing a multitenant app following this article. The problem is when I run all migrations the first time. In schema.rb file are only the tables for the public schema but what happend with the others schemas? and how can I create others schemas with different structure to the public I don't want to use gems.

See the example below

Table to be created for public schema

class CreatePerspectives < ActiveRecord::Migration
  include MultiSchema
  def up
      with_in_schemas :only => :public do
         # Create table perspectives
      end
  end


  def down
    with_in_schemas :only => :public do
      drop_table :prespectives
    end
  end
end

Table to be created for private schemas

class CreateObjectives < ActiveRecord::Migration

  include MultiSchema

  def change
    with_in_schemas :except => :public do
        # Create objectives table
    end
  end
end

schema.rb

ActiveRecord::Schema.define(version: 20130810172443) do

  create_table "perspectives", force: true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
end
Chris Travers
  • 25,424
  • 6
  • 65
  • 182
Cristhian Boujon
  • 4,060
  • 13
  • 51
  • 90
  • I don't understand why you need multiple schemas? Can't you use just one and make certain information inaccessible to users based on their role? – Justus Eapen Feb 24 '14 at 02:32
  • I've used and loved Postgres for years and am very familiar with schemas and their usage. I'm also a Rails developer. That said, I would *never* use the two together unless I had to. You don't have to. Simply design out your data layout (normally I would call this a "schema" but I don't want to confuse the issue) and add another table called "company" or whatever owns the records. Then give it ownership within various tables. I have a few apps like this and it's not difficult. – Michael Chaney Oct 07 '14 at 14:24

1 Answers1

2

do you know Apartment gem? https://github.com/influitive/apartment

I used in a project on this year, that one supports multi schema with postgresql. Everything is documented and easy to use. And you can chose the Middleware mode. For example, when you access the domain customer.applicationdomain.com the application can select the right schema for you. By the way you can use background jobs with sidekiq too.

Ivan Santos
  • 626
  • 2
  • 7
  • 19