This website uses cookies to better the user experience of its visitors. Where applicable, this website uses a cookie control system, allowing users to allow or disallow the use of cookies on their computer/device on their first visit to the website. This complies with recent legislative requirements for websites to obtain explicit consent from users before leaving behind or reading files such as cookies on a user’s computer/device. To learn more click Cookie Policy.

Privacy preference center

Cookies are small files saved to a user’s computer/device hard drive that track, save, and store information about the user’s interactions and website use. They allow a website, through its server, to provide users with a tailored experience within the site. Users are advised to take necessary steps within their web browser security settings to block all cookies from this website and its external serving vendors if they wish to deny the use and saving of cookies from this website to their computer’s/device’s hard drive. To learn more click Cookie Policy.

Manage consent preferences

These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
Cookies list
Name _rg_session
Provider rubygarage.org
Retention period 2 days
Type First party
Category Necessary
Description The website session cookie is set by the server to maintain the user's session state across different pages of the website. This cookie is essential for functionalities such as login persistence, ensuring a seamless and consistent user experience. The session cookie does not store personal data and is typically deleted when the browser is closed, enhancing privacy and security.
Name m
Provider m.stripe.com
Retention period 1 year 1 month
Type Third party
Category Necessary
Description The m cookie is set by Stripe and is used to help assess the risk associated with attempted transactions on the website. This cookie plays a critical role in fraud detection by identifying and analyzing patterns of behavior to distinguish between legitimate users and potentially fraudulent activity. It enhances the security of online transactions, ensuring that only authorized payments are processed while minimizing the risk of fraud.
Name __cf_bm
Provider .pipedrive.com
Retention period 1 hour
Type Third party
Category Necessary
Description The __cf_bm cookie is set by Cloudflare to support Cloudflare Bot Management. This cookie helps to identify and filter requests from bots, enhancing the security and performance of the website. By distinguishing between legitimate users and automated traffic, it ensures that the site remains protected from malicious bots and potential attacks. This functionality is crucial for maintaining the integrity and reliability of the site's operations.
Name _GRECAPTCHA
Provider .recaptcha.net
Retention period 6 months
Type Third party
Category Necessary
Description The _GRECAPTCHA cookie is set by Google reCAPTCHA to ensure that interactions with the website are from legitimate human users and not automated bots. This cookie helps protect forms, login pages, and other interactive elements from spam and abuse by analyzing user behavior. It is essential for the proper functioning of reCAPTCHA, providing a critical layer of security to maintain the integrity and reliability of the site's interactive features.
Name __cf_bm
Provider .calendly.com
Retention period 30 minutes
Type Third party
Category Necessary
Description The __cf_bm cookie is set by Cloudflare to distinguish between humans and bots. This cookie is beneficial for the website as it helps in making valid reports on the use of the website. By identifying and managing automated traffic, it ensures that analytics and performance metrics accurately reflect human user interactions, thereby enhancing site security and performance.
Name __cfruid
Provider .calendly.com
Retention period During session
Type Third party
Category Necessary
Description The __cfruid cookie is associated with websites using Cloudflare services. This cookie is used to identify trusted web traffic and enhance security. It helps Cloudflare manage and filter legitimate traffic from potentially harmful requests, thereby protecting the website from malicious activities such as DDoS attacks and ensuring reliable performance for genuine users.
Name OptanonConsent
Provider .calendly.com
Retention period 1 year
Type Third party
Category Necessary
Description The OptanonConsent cookie determines whether the visitor has accepted the cookie consent box, ensuring that the consent box will not be presented again upon re-entry to the site. This cookie helps maintain the user's consent preferences and compliance with privacy regulations by storing information about the categories of cookies the user has consented to and preventing unnecessary repetition of consent requests.
Name OptanonAlertBoxClosed
Provider .calendly.com
Retention period 1 year
Type Third party
Category Necessary
Description The OptanonAlertBoxClosed cookie is set after visitors have seen a cookie information notice and, in some cases, only when they actively close the notice. It ensures that the cookie consent message is not shown again to the user, enhancing the user experience by preventing repetitive notifications. This cookie helps manage user preferences and ensures compliance with privacy regulations by recording when the notice has been acknowledged.
Name referrer_user_id
Provider .calendly.com
Retention period 14 days
Type Third party
Category Necessary
Description The referrer_user_id cookie is set by Calendly to support the booking functionality on the website. This cookie helps track the source of referrals to the booking page, enabling Calendly to attribute bookings accurately and enhance the user experience by streamlining the scheduling process. It assists in managing user sessions and preferences during the booking workflow, ensuring efficient and reliable operation.
Name _calendly_session
Provider .calendly.com
Retention period 21 days
Type Third party
Category Necessary
Description The _calendly_session cookie is set by Calendly, a meeting scheduling tool, to enable the meeting scheduler to function within the website. This cookie facilitates the scheduling process by maintaining session information, allowing visitors to book meetings and add events to their calendars seamlessly. It ensures that the scheduling workflow operates smoothly, providing a consistent and reliable user experience.
Name _gat_UA-*
Provider rubygarage.org
Retention period 1 minute
Type First party
Category Analytics
Description The _gat_UA-* cookie is a pattern type cookie set by Google Analytics, where the pattern element in the name contains the unique identity number of the Google Analytics account or website it relates to. This cookie is a variation of the _gat cookie and is used to throttle the request rate, limiting the amount of data collected by Google Analytics on high traffic websites. It helps manage the volume of data recorded, ensuring efficient performance and accurate analytics reporting.
Name _ga
Provider rubygarage.org
Retention period 1 year 1 month 4 days
Type First party
Category Analytics
Description The _ga cookie is set by Google Analytics to calculate visitor, session, and campaign data for the site's analytics reports. It helps track how users interact with the website, providing insights into site usage and performance.
Name _ga_*
Provider rubygarage.org
Retention period 1 year 1 month 4 days
Type First party
Category Analytics
Description The _ga_* cookie is set by Google Analytics to store and count page views on the website. This cookie helps track the number of visits and interactions with the website, providing valuable data for performance and user behavior analysis. It belongs to the analytics category and plays a crucial role in generating detailed usage reports for site optimization.
Name _gid
Provider rubygarage.org
Retention period 1 day
Type First party
Category Analytics
Description The _gid cookie is set by Google Analytics to store information about how visitors use a website and to create an analytics report on the website's performance. This cookie collects data on visitor behavior, including pages visited, duration of the visit, and interactions with the website, helping site owners understand and improve user experience. It is part of the analytics category and typically expires after 24 hours.
Name _dc_gtm_UA-*
Provider rubygarage.org
Retention period 1 minute
Type First party
Category Analytics
Description The _dc_gtm_UA-* cookie is set by Google Analytics to help load the Google Analytics script tag via Google Tag Manager. This cookie facilitates the efficient loading of analytics tools, ensuring that data on user behavior and website performance is accurately collected and reported. It is categorized under analytics and assists in the seamless integration and functioning of Google Analytics on the website.

How We Retrieve Tenant Data in a Multi-Tenant App with Detectify

  • 23483 views
  • 5 minutes
Fedor L.

Fedor L.

Ruby/JS Developer

Sviatoslav A.

Sviatoslav A.

Copywriter

Share

We often build multi-tenant applications for Software as a Service (SaaS) providers. As we know, support for multi-tenancy is imperative for a SaaS application because a single instance of the app must manage data for multiple clients. The difference between a multi-tenant and a single-tenant app chiefly concerns the database tier, or layer. Multi-tenant and single-tenant refers to how client data is stored and accessed in a database.

But this article won’t talk about how to segregate client data in a database schema (or schemas) with Ruby gems, such as Apartment and Multitenancy. Instead, we’ll explain how to work around a different challenge – retrieving specific data depending on the tenant’s domain or subdomain name. We’ll present our own solution for this task, which you can use to build your own multi-tenant SaaS application.

Identifying the Tenant in a Multi-Tenant App

We often see URLs similar to these: tenant-one.webapp.com and tenant-two.webapp.com. For example, Slack allocates the rubygarage.slack.com subdomain for our RubyGarage team, and we must enter our subdomain – rubygarage – to sign in. This approach to building a multi-tenant web application can be called partial white-labeling (i.e. we don’t get our own full domain for Slack).

Entering subdomain in Slack

We commonly retrieve a tenant’s data (an entity) from Active Record using a subdomain name in our SaaS projects.

For example, we’re working on a project called Shopperations, a SaaS application designed for shopper marketers. Shopperations allows multiple tenants (companies) to separately work on their respective marketing projects. When a Shopperations user (a company representative) wants to sign in to the Shopperations application, they have to enter their subdomain.

Entering subdomain in Shopperations

So what happens when we enter a subdomain name and sign in? Put simply, the application determines who we are using this subdomain name and loads our data.

The responsible code grabs this subdomain and sends a request to the database. Since database schemas are set up correctly, only our data is retrieved and we are redirected to our unique page. It’s as simple as that!

How We Identified Tenants Before

In order to send a subdomain-specific request and receive appropriate data, we’ve typically employed a Ruby gem called Houser. But several months ago we decided to switch to a custom solution for a couple of reasons.

First, Houser can look for data only using subdomain names, whereas we needed to search for data using domain names as well. Second, we wanted to be able to ignore routes when sending a URL-based request to the database, and Houser didn’t let us do this.

Before building Detectify, we had to write our own code on top of Houser to implement these two features we just mentioned. But this implementation required two requests to the database. Since we wanted to be efficient – meaning we wanted to use a single request to retrieve data – and since we needed a more flexible solution, we decided to build Detectify.

What’s Detectify?

Detectify is a simple Ruby gem, similar to Houser, that helps you retrieve an Active Record entity from a database with a URL. The URL can be either a domain name or subdomain name.

It’s easy to install and set up Detectify to meet your specific needs. Below, you’ll find an explanation of what Detectify does and how exactly the gem does it. Keep in mind that Detectify is intended for use with Rack applications.

Detectify: How It Works

To use Detectify, you must first add the following line to your project’s Gemfile:

You’ll also need to install Detectify with the standard command:

Install Detectify with the help of Bundler.

Keep in mind that Detectify only supports applications built with Ruby 2.2.2 and Rails 4.2 or higher. There’s no need to install any other gems to run Detectify.

Now, let’s take a look at the short Middleware module that our developer created for Detectify.

Detectify’s workflow starts in the middleware.rb file. Our middleware receives a new Rack request and initializes the Detector (the main Detectify entity) with the URL returned by that Rack request. Next, Detectify sends a request to the next middleware, where the result of the initial request is actually used.

The detector.rb file is the second stop in Detectify’s workflow. The Detector class receives a URL and builds a request to the database using QueryBuilder.

For now, we have a single query builder for SQL-type databases. But the query builder implementation allows you to freely add query builders to Detectify for other types of databases.

Let’s also have a look at the QueryBuilder code.

The QueryBuilder module allows us to build a more advanced request to the database than what Active Record normally does. This is why we’ve added such functions as domain_clause and subdomain_clause to this module.

To finish setting up Detectify in your SaaS app, you’ll also need to specify the Active Record model that will be used for search in configs.

Detectify helps implement multi-tenancy in Software as a Service applications. Our gem is an efficient solution for retrieving tenant data with the Rack request, which is created based on the domain or subdomain name of the tenant.

Admittedly, we could simply add several lines of code to implement Detectify’s functionality in every new Software as a Service project. But we believe it’s best to use a scalable solution that can be easily modified to work with more than just SQL-type databases.

Go ahead and try out Detectify in your own projects, and extend it if necessary. And if you have any suggestions for changes, commit to Detectify on GitHub.

CONTENTS

Authors:

Fedor L.

Fedor L.

Ruby/JS Developer

Sviatoslav A.

Sviatoslav A.

Copywriter

Rate this article!

Nay
So-so
Not bad
Good
Wow
1 rating, average 5 out of 5

Share article with

Comments (2)
Chetan Rana
Chetan Rana almost 7 years ago
Can i use detectify along with Apartment gem?
Reply
Maryna Z.
Maryna Z. almost 7 years ago
Hi there and thank you for your question. Using Apartment gem along with Detectify might be cumbersome. I do recommend you to do additional research on both gems and decide what functionality you need. The Apartment gem has all the features of Detectify and even more. So, if you need that extra functionality that's not present in Detectify, stick to Apartment gem then. I hope I was hepful!
Reply

Subscribe via email and know it all first!