【问题标题】:Seeding data with Apartment gem for new schemas?使用 Apartment gem 为新模式播种数据?
【发布时间】:2017-06-18 04:09:45
【问题描述】:

如何在使用 Apartment gem 进行范围界定的多租户应用程序中播种新租户?

我在 seed.rb 中添加了以下内容,但它似乎不适用于公寓:

tenants = Tenant.create([
{
  name: 'User1',
  domain: 'user1'
},
{
  name: 'User2',
  domain: 'user2'
}
])

即使数据库是空白的,seeds.rb 也会尝试两次创建架构,但在我执行 rake db:seed 时失败并出现此错误:

Apartment::TenantExists: The schema user1 already exists.

【问题讨论】:

  • 希望你没有忘记做 rake db:seed
  • 是的,当我 rake db:seed 时会发生错误。我已经更新了问题。
  • 您的问题可能重复。见这里:stackoverflow.com/a/27343169/1322092

标签: ruby-on-rails ruby multi-tenant apartment-gem


【解决方案1】:

Rails rake 为您尝试创建的每个租户/模式运行任务。因此,首先检查当前架构是否是公共的,从而运行您的创建或播种任务。

这样的事情就可以了。

if Apartment::Tenant.current == 'public'
  Apartment::Tenant.create('tenant1')
  Apartment::Tenant.create('tenant2')
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    • 1970-01-01
    • 2016-12-10
    • 2017-10-05
    相关资源
    最近更新 更多