DEV Community

Discussion on: Simple Multitenancy in Rails

Collapse
 
rhs0112 profile image
rhs0112

Thanks for posting this. Could you also provide on example (of a model) that uses this concern to ensure multi tenancy?

Collapse
 
ritikesh profile image
Ritikesh

Hey, Thanks for reading. You can easily include the concerns and use them as you go. For eg.)

If you have a user class that you want to make current'able, add the following code:

# app/models/user.rb

class User < ActiveRecord::Base
  include ActsAsCurrent
end

# app/helpers/application_helper.rb

class ApplicationHelper
  def current_user
    return User.current if User.current
    User.find(session[:id]).make_current
  end
end

# app/controllers/application_controller.rb

class ApplicationController < ActionController::Base

  after_filter :reset_current_user

  private
  def reset_current_user
    User.reset_current
  end
end