influitive / apartment

Database multi-tenancy for Rack (and Rails) applications
2.66k stars 464 forks source link

Question - how to quickly switch tenant for rake task/rails c/etc? #641

Closed rusikf closed 4 years ago

rusikf commented 4 years ago

Hi, now it is possible to switch tenant :

Apartment::Tenant.switch('google') { Page.all }

But I want run rails console in google ( I am to lazy to write every case Apartment::Tenant.switch ) Is it possible to setup specific ENV, so I can run rails s, bundle exec rake some_task in context of tenant ?( google in this example)

lcjury commented 4 years ago

on your application.rb

  console do
      if ENV["TENANT"]
        Apartment::Tenant.switch!(ENV["TENANT"])
        Rails.logger.info "Connected to tenant #{ENV["TENANT"]}"
      end
    end

Now you can do this: TENANT=name_of_tenant ./bin/rails c

rusikf commented 4 years ago

Cool, Thanks for response! :+1: