3

我已经设置了架构文件,但无法为租户定义种子文件,以便它只能为租户迁移运行。此外,一旦创建了用户并创建了租户,我也会尝试创建模式。

    require 'apartment/elevators/subdomain'

    #
    # Apartment Configuration
    #
    Apartment.configure do |config|

      config.excluded_models = ["Admin","Contractor", "ContractorPackage","ContractorTransaction","Country","Currency","Faq","FaqCategory","Language","Package","Page","PaymentType","Setting","TempTransaction","Testimonial","Timezone","Tutorial"]

      # use postgres schemas?
      config.use_schemas = true

       config.tenant_names = lambda{ Contractor.pluck("CONCAT('contractor_',id)") }
    end

    # overriding module schema file here
    module Apartment

      class << self
            def database_schema_file
                @database_schema_file=Rails.root.join('db', 'contractor_schema.rb')
            end
        end

    end


    Rails.application.config.middleware.use 'Apartment::Elevators::Subdomain'
4

1 回答 1

17

在您的种子.rb 文件中,将您的代码包装在当前租户的检查中。我现在没有任何地方可以测试这个,但是下面的代码应该让你接近:

unless Apartment::Tenant.current == 'public'
    #Insert seed data
end

如果您想手动为租户播种,您应该能够运行Apartment::Tenant.seed

要在创建租户时运行 seed.rb 文件,请添加:

config.seed_after_create = true

到你的公寓初始化文件。

对于您的示例:

Apartment.configure do |config|

  config.excluded_models = ["Admin","Contractor", "ContractorPackage","ContractorTransaction","Country","Currency","Faq","FaqCategory","Language","Package","Page","PaymentType","Setting","TempTransaction","Testimonial","Timezone","Tutorial"]

  # use postgres schemas?
  config.use_schemas = true

  config.tenant_names = lambda{ Contractor.pluck("CONCAT('contractor_',id)") }

  config.seed_after_create = true
end
于 2014-12-07T13:35:06.567 回答