Skip to content

Instantly share code, notes, and snippets.

@dpickett
Last active October 3, 2018 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpickett/240a3c473c8d0f95e730f58533b75762 to your computer and use it in GitHub Desktop.
Save dpickett/240a3c473c8d0f95e730f58533b75762 to your computer and use it in GitHub Desktop.
Multitenant
module MultitenantBehaviors
protected
def current_company
if request.subdomain && request.subdomain != 'www'
# assumes a `subdomain` string field on the Company class / table
@current_company ||= Company.find_by(subdomain: request.subdomain)
else
nil
end
end
end
class WidgetsController < ApplicationController
include MultitenantBehaviors
def index
if current_company
#assumes an association between Company objects and Widget objects
@widgets = current_company.widgets
else
@widgets = Widget.all
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment