如何为Rails Apartment定义种子文件
我已经设置了模式文件,但无法为租户定义种子文件,因此它只能为租户迁移运行。 此外,我还尝试在创建用户并创建其租户后创建架构。
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文件中,将代码包装在当前租户的检查中。 我现在没有任何地方可以测试这个,但下面的代码应该让你关闭:
unless Apartment::Tenant.current_tenant == 'public' #Insert seed data end
如果您想手动为租户播种,您应该能够运行Apartment::Tenant.seed
要在创建租户时运行seeds.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