0
votes

I created a multi tenancy app with Apartment gem and rails 5. I successfully create a new tenant, but I want to seed it. When I run the seeds file it states that the seed has run for this new tenant (Seeding tenant_name tenant), but there's no data there, only on public schema. I can see 2 schemas created on PostgreSQL db,the public and the new one, but it only populates public schema. Why?

Tried putting on seeds.rb:

Apartment::Tenant.switch!('tenant_name')

And:

if Apartment::Tenant.current == "tenant_name"...

But no good.

Anyone?

Thanks in advance!

1

1 Answers

1
votes

Your approach is correct, but please make sure these:

  1. Ensure schema_search_path in PG:

Example: database.yml should look like:

default: &default
  adapter: postgresql
  schema_search_path: 'public,shared_extensions'
  encoding: unicode
  pool: 5
  prepared_statements: false

development:
  <<: *default
database: your_development_db

2. For schema-specific data population, run statement inside tenant switch block:

In seed.rb, first create tenant, then switch in that tenant like this:

Apartment::Tenant.switch('tenant_name') do
  # Do all stuff here inside this block
  # User.create(user_attributes) will create use only inside `tenant_name` schema
end

Cheer!