{"id":13879918,"url":"https://github.com/salsify/rails-multitenant","last_synced_at":"2025-06-10T15:38:56.100Z","repository":{"id":3118291,"uuid":"48440165","full_name":"salsify/rails-multitenant","owner":"salsify","description":null,"archived":false,"fork":false,"pushed_at":"2024-11-08T17:30:12.000Z","size":6846,"stargazers_count":41,"open_issues_count":4,"forks_count":0,"subscribers_count":61,"default_branch":"master","last_synced_at":"2025-05-26T12:19:20.989Z","etag":null,"topics":["gem"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/salsify.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-12-22T15:44:12.000Z","updated_at":"2025-03-14T19:42:24.000Z","dependencies_parsed_at":"2024-10-17T23:52:03.767Z","dependency_job_id":"40afd61e-10b5-4cb0-99c0-011598b82f13","html_url":"https://github.com/salsify/rails-multitenant","commit_stats":{"total_commits":53,"total_committers":13,"mean_commits":4.076923076923077,"dds":0.7169811320754718,"last_synced_commit":"76bee9b3bdedece5a26d908327111681cfece0b9"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/salsify%2Frails-multitenant","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/salsify%2Frails-multitenant/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/salsify%2Frails-multitenant/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/salsify%2Frails-multitenant/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/salsify","download_url":"https://codeload.github.com/salsify/rails-multitenant/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/salsify%2Frails-multitenant/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259104048,"owners_count":22805798,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gem"],"created_at":"2024-08-06T08:02:38.962Z","updated_at":"2025-06-10T15:38:56.047Z","avatar_url":"https://github.com/salsify.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# RailsMultitenant\n\n[![Gem Version](https://badge.fury.io/rb/rails_multitenant.svg)][gem]\n[![Build Status](https://circleci.com/gh/salsify/rails-multitenant.svg?style=svg)][circleci]\n\n[gem]: https://rubygems.org/gems/rails_multitenant\n[circleci]: https://circleci.com/gh/salsify/rails-multitenant\n\nrails_multitenant is a gem for isolating ActiveRecord models from different tenants. The gem assumes tables storing\nmulti-tenant models include an appropriate tenant id column.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'rails_multitenant'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install rails_multitenant\n\nIf you're using Rails, there's nothing else you need to do.\n\nOtherwise, you need to insert `RailsMultitenant::Middleware::IsolatedContextRegistry` into your middleware stack\n\n## Usage\n\nThe gem supports two multi-tenancy strategies:\n\n1. Based on a model attribute, typically a foreign key to an entity owned by another service\n2. Based on a model association\n\nThe gem uses ActiveRecord default scopes to make isolating tenants fairly transparent.\n\n### Multi-tenancy Based on Model Attributes\n\nThe following model is multi-tenant based on an `organization_id` attribute:\n\n```ruby\nclass Product \u003c ActiveRecord::Base\n  include RailsMultitenant::MultitenantModel\n\n  multitenant_on :organization_id\nend\n```\n\nThe model can then be used as follows:\n\n```ruby\nRailsMultitenant::GlobalContextRegistry[:organization_id] = 'my-org'\n\n# Only returns products from 'my-org'\nProduct.all\n\n# Returns products across all orgs\nProduct.strip_organization_scope.all\n\n# Or set the current organization in block form\nRailsMultitenant::GlobalContextRegistry.with_isolated_registry(organization_id: 'my-org') do\n  # Only returns products from 'my-org'\n  Product.all\nend\n```\n\nBy default this adds an ActiveRecord validation to ensure the multi-tenant attribute is present but this can be disabled\nby passing `required: false` to `multitenant_on`.\n\n### Multi-tenancy Based on Associated Models\n\nThe following model is multi-tenant based on an `Organization` model:\n\n```ruby\nclass Product \u003c ActiveRecord::Base\n  include RailsMultitenant::MultitenantModel\n\n  multitenant_on_model :organization\nend\n```\n\nThe model can then be used as follows:\n\n```ruby\nOrganization.current_id = 1\n\n# Only returns products from organization 1\nProduct.all\n\n# Use the automatically generated belongs_to association to get\n# a product's organization\nProduct.first.organization\n\n# Or set the current organization in block form\nOrganization.as_current_id(1) do\n  # Only returns products from organization 1\n  Product.all\nend\n```\n\nBy default this adds an ActiveRecord validation to ensure the tenant model is present but this can be disabled\nby passing `required: false` to `multitenant_on_model`.\n\n### `current` Models\n\nClasses can be enabled to have current, thread-local instances. For a standard class this is done with:\n\n```ruby\nclass MyClass\n  include RailsMultitenant::GlobalContextRegistry::Current\nend\n\nMyClass.current = MyClass.new\n```\n\nFor an `ActiveRecord` model you can use the following, which additionally allows storing the current model ID.\n\n```ruby\nclass MyClass\n  include RailsMultitenant::GlobalContextRegistry::CurrentInstance\nend\n\nMyClass.current_id = 123\nMyClass.current # =\u003e #\u003cMyClass id: 123\u003e\n```\n\n#### Dependency Tracking\n\nFor classes that are dependent on other `Current` classes you can register dependencies.\n\n```ruby\nclass DependentClass\n  include RailsMultitenant::GlobalContextRegistry::Current\n  provide_default :default\n  global_context_dependent_on MyClass\n\n  def self.default\n    new(MyClass.current.dependent_id)\n  end\n\n  def initialize(id)\n    @id = id\n  end\nend\n```\n\nWhen doing so, clearing the `current` class on the referenced class will also clear the `current` context of the dependent class.\n\n```ruby\nklass = MyClass.new\nMyClass.current = klass\nDependentClass.current # =\u003e #\u003cDependentClass id: klass.dependent_id\u003e\n\nMyClass.current = nil\nDependentClass.current # =\u003e nil\n```\n\nFor bi-directional dependencies you can use `#global_context_mutually_dependent_on` instead of `#global_context_dependent_on`.\n\n### Shorthand\n\nWhen using `rails-multitenant` in a project, it is common to need to set values in `RailsMultitenant::GlobalContextRegistry` at the rails console.\n\nThis is difficult to type. Alternatively you can shorten it to `RailsMultitenant`. For example you might type `RailsMultitenant[:organization_id] = 'some value'` and it will have the same effect as the long version.\n\nThis is mainly intended as a console convenience. Using the long form in source code is fine, and more explicit.\n\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/salsify/rails-multitenant. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsalsify%2Frails-multitenant","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsalsify%2Frails-multitenant","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsalsify%2Frails-multitenant/lists"}