Rails多租户播种问题
问题描述:
我用Apartment gem和rails 5创建了一个多租户应用程序。我成功创建了一个新租户,但我想播种它。当我运行种子文件时,它声明种子已经为这个新租户运行(种子tenant_name租户),但是那里没有数据,只有在公共模式上。 我可以看到在PostgreSQL
db,public
和新建的模式上创建的2个模式,但它只填充公共模式。为什么?Rails多租户播种问题
试图把对seeds.rb
:
Apartment::Tenant.switch!('tenant_name')
和:
if Apartment::Tenant.current == "tenant_name"...
但没有好。
有人吗?
在此先感谢!
答
你的做法是正确的,但请确保这些:
- 确保
schema_path
在PG:
举例:database.yml
应该是这样的:
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
- 具体数据大全对租户switch
块内,运行声明:
在seed.rb
,首先创建租户,然后在租户切换这样的:
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
欢呼!