如何为 Rails 公寓定义种子文件 [英] How to define seed file for Rails Apartment

查看:34
本文介绍了如何为 Rails 公寓定义种子文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了架构文件,但无法为租户定义种子文件,因此它只能为租户迁移运行.此外,一旦创建了用户并创建了其租户,我就会尝试创建架构.

I have set up the schema file but unable to define seed file for tenant such that it can run only for tenant migration only. Also I ma trying to create schema once a user has been created and its tenant is created.

    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'

推荐答案

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

In your seeds.rb file, wrap your code in a check for the current tenant. I don't have anywhere to test this right now, but the following code should get you close:

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

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

If you want to seed a tenant manually you should be able to run Apartment::Tenant.seed

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

To get the seeds.rb file to run when a tenant is created add:

config.seed_after_create = true

到你的公寓初始化文件.

to your apartment initializer file.

例如:

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

这篇关于如何为 Rails 公寓定义种子文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆