RailsCasts Pro episodes are now free!

Learn more or hide this

Ole Henrik Skogstrøm's Profile

GitHub User: ohenrik

Comments by Ole Henrik Skogstrøm

Avatar

Hi Ryan

Just wanted to say thank you very much for an excellent video. Hope you are doing well and that you soon have enough energy to return to railscast.

Avatar

It is actually not that hard to do. I was worried about this step but the solution was straight forward.

ruby
        def current_tenant
                @current_tenant = User.find_by_alias_domain(request.host) unless %w(lvh.me other_domains_you_own.com).include?(request.domain)
                
                @current_tenant ||= User.find_by_subdomain!(request.subdomain) # includes(:home).
        end
        helper_method :current_tenant

Just remember to do this kind to double check every where you are checking for subdomain. Also you need to be shure your webserver (nginx for example) accepts all domains (this is the default if you have server by your self, do not know how this works on heroku)

Avatar

If anyone are looking for a way to exclude new tables from the multitenant migration (i.e. Only create the new table in the public schema). This solved the problem for me:

ruby
if ActiveRecord::Base.connection.current_schema == 'public'
Your migration 
end

Complete example

ruby
class UsersHasAndBelongsToManyRoles < ActiveRecord::Migration
        def change
                if ActiveRecord::Base.connection.current_schema == 'public'
                        create_table :roles do |t|
                                t.string :role
                                t.timestamps
                        end

                        create_table :roles_users do |t|
                                t.belongs_to :user, :null => false
                                t.belongs_to :role, :null => false
                        end

                end
        end
end
Avatar

Thank you!

If i deploy this application. will i be able to do the same change to a similar file on a linux server?