How to define seed file for Rails Apartment

六眼飞鱼酱① 提交于 2019-12-05 06:19:40

问题


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'

回答1:


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

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

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

config.seed_after_create = true

to your apartment initializer file.

For your example:

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


来源:https://stackoverflow.com/questions/27342770/how-to-define-seed-file-for-rails-apartment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!