【问题标题】:How to define seed file for Rails Apartment如何为 Rails 公寓定义种子文件
【发布时间】:2014-12-07 12:53:38
【问题描述】:

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

    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'

【问题讨论】:

  • Rake db:seed 会做到的。
  • 它也会为公共模式播种。我只想为用户/租户运行
  • 这里的代码很多,大部分都是cmets;您能否清除 cmets 以简化您所呈现的内容并将代码限制为直接适用于问题的内容(可能是此处的所有代码)。
  • 你能让它工作吗?

标签: ruby-on-rails ruby-on-rails-4 apartment-gem


【解决方案1】:

在您的种子.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

【讨论】:

  • 但是我们如何从模型中运行种子命令呢?假设在用户模块中有一个函数 after_create: create_schema def create_schema #create tenant command #>> 我怎样才能在这里运行种子命令??????结束
  • 刚刚添加了有关如何执行此操作的信息
  • 您能否在问题中添加实际错误以及导致错误的相关代码?
  • @rdubya 的答案非常好。但我需要一些更像是在后台运行种子的东西。因为我需要用他们所有的状态和其他一些东西来播种世界上所有的国家。完成种子需要将近 3 分钟,并且用户在所有种子完成之前不会收到通知。所以我需要在后台运行它。对此有任何建议。
  • 条件的正确写法其实是unless Apartment::Tenant.current == 'public'(skip_tenant)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多