0

I have subdomains like: lks.harabiz.com & lkm.harabiz.com, each with its own database tables.

When somebody tries the URL: www.lkm.harabiz.com, the database tables being used are different from lkm's, and those database tables actually belong to www, not to lkm.

There are two solutions to this problem:

  1. Point www.lkm.harabiz.com to simply lkm.harabiz.com.
  2. Make www.lkm.harabiz.com use the database tables that actually belong to lkm, and not to www.

I'm using Rails 5.0.0.1, and the app is deployed through Heroku.

I have looked the DNS stuff, plus stuff on application level, but I'm unable to achieve the desired results.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76

5 Answers5

2

You can use a Custom Elevator:

# application.rb
module MyApplication
  class Application < Rails::Application
    config.middleware.use Apartment::Elevators::Generic, Proc.new { |request| request.host.split('.')[-3] }
  end
end
wesley6j
  • 1,393
  • 9
  • 17
0

Simply increase Top Level Domain by 1:

# config/environments/production.rb
Rails.application.configure do
  config.action_dispatch.tld_length = 2
end

More information about this option is there: http://guides.rubyonrails.org/configuring.html

And there's a little description about how it works: http://api.rubyonrails.org/classes/ActionDispatch/Http/URL.html

itsnikolay
  • 17,415
  • 4
  • 65
  • 64
0

you can trap from your config/routes.rb file with this code below

Rails.application.routes.draw do
  get '', to: 'lkm_controllers#index', constraints: lambda { |r|
   r.subdomain.present? && r.subdomain == "www.lkm"
  }
  get '', to: 'lks_controllers#index', constraints: lambda { |r|
   r.subdomain.present? && r.subdomain == "www.lks"
  }
  # other resources
end

make sure these 2 commands you put above, so it checked it first whether it's using www.lkm or just lkm

widjajayd
  • 6,090
  • 4
  • 30
  • 41
0

The way to use those different tables is application specific, so you shouldnt add any DNS interaction code because it will make your code fragile and less portable, NOR modify DNS options in heroku or your DNS entries.

What you need is to make use your desired database based on the host of the HTTP request arriving your app. Look at Rails.application.routes and https://apidock.com/rails/ActionController/AbstractRequest/request_uri to do it. Read about request routing for your framework. (or put here your routing code to get more help)

0

Change the apartment gem configuration and add 'www' to exclude_domains in config/initializers/apartment/subdomain_exclusions.rb:

Apartment::Elevators::Subdomain.excluded_subdomains = ['www']
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68