{"id":18513342,"url":"https://github.com/jbox-web/store_base_sti_class","last_synced_at":"2025-06-10T19:35:03.538Z","repository":{"id":199243012,"uuid":"702441400","full_name":"jbox-web/store_base_sti_class","owner":"jbox-web","description":null,"archived":false,"fork":false,"pushed_at":"2025-03-04T21:10:08.000Z","size":48,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-14T12:19:52.441Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/jbox-web.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-10-09T10:21:24.000Z","updated_at":"2025-03-04T21:10:12.000Z","dependencies_parsed_at":"2024-09-06T18:55:00.835Z","dependency_job_id":"9aafc7a6-760a-41a1-be91-3859ccae0484","html_url":"https://github.com/jbox-web/store_base_sti_class","commit_stats":null,"previous_names":["jbox-web/store_base_sti_class"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbox-web%2Fstore_base_sti_class","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbox-web%2Fstore_base_sti_class/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbox-web%2Fstore_base_sti_class/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbox-web%2Fstore_base_sti_class/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jbox-web","download_url":"https://codeload.github.com/jbox-web/store_base_sti_class/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbox-web%2Fstore_base_sti_class/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259136932,"owners_count":22810568,"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":[],"created_at":"2024-11-06T15:37:45.204Z","updated_at":"2025-06-10T19:35:03.484Z","avatar_url":"https://github.com/jbox-web.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# StoreBaseSTIClass\n\nActiveRecord has always stored the base class in `polymorphic_type` columns when using STI. This can have non-trivial\nperformance implications in certain cases. This gem adds the `store_base_sti_class` configuration option which controls\nwhether ActiveRecord will store the base class or the actual class. Defaults to true for backwards compatibility.\n\n## Description\n\nGiven the following class definitions:\n\n```ruby\nclass Address\n  belongs_to :addressable, polymorphic: true\nend\n\nclass Person\n  has_many :addresses, as: addressable\nend\n\nclass Vendor \u003c Person\nend\n```\n\nand given the following code:\n\n```ruby\nvendor = Vendor.create(...)\naddress = vendor.addresses.create(...)\n\np vendor\np address\n```\n\nwill output:\n\n```ruby\n#\u003cVendor id: 1, type: \"Vendor\" ...\u003e\n#\u003cAddress id: 1, addressable_id: 1, addressable_type: 'Person' ...\u003e\n```\n\nNotice that `addressable_type` column is Person even though the actual class is Vendor.\n\nNormally, this isn't a problem, however, it can have negative performance characteristics in certain circumstances. The\nmost obvious one is that a join with persons or an extra query is required to find out the actual type of addressable.\n\nThis gem adds the `ActiveRecord::Base.store_base_sti_class` configuration option. It defaults to true for backwards\ncompatibility. Setting it to false will alter ActiveRecord's behavior to store the actual class in `polymorphic_type`\ncolumns when STI is used.\n\nIn the example above, if the `ActiveRecord::Base.store_base_sti_class` is `false`, the output will be,\n\n```ruby\n#\u003cVendor id: 1, type: \"Vendor\" ...\u003e\n#\u003cAddress id: 1, addressable_id: 1, addressable_type: 'Vendor' ...\u003e\n```\n\n## Usage\n\nAdd the following line to your Gemfile\n\n```ruby\ngem 'store_base_sti_class'\n```\n\nthen bundle install. Once you have the gem installed, add the following to one of the initializers (or make a new one)\nin config/initializers,\n\n```ruby\nActiveRecord::Base.store_base_sti_class = false\n```\n\nWhen changing this behavior, you will have write a migration to update all of your existing `_type` columns accordingly.\nYou may also need to change your application if it explicitly relies on the `_type` columns.\n\n## Notes\n\nThis gem incorporates work from:\n\n- https://github.com/codepodu/store_base_sti_class_for_4_0\n\nIt currently works with ActiveRecord `4.2.x` through `7.0.x`. If you need support for ActiveRecord `3.x`, use a\n`pre-1.0` version of the gem, or ActiveRecord \u003c `4.2` use a `pre-2.0` version of the gem, or ActiveRecord \u003c `6` use\nversion \u003c `3` of the gem.\n\n## Conflicts\n\nThis gem produces known conflicts with these other gems:\n\nWhen using [friendly_id](https://github.com/norman/friendly_id) \u003e= `5.2.5` with the\n[History module](https://norman.github.io/friendly_id/FriendlyId/History.html) enabled, duplicate slugs will be\ngenerated for STI subclasses with the same sluggable identifier (ex: name). This will either cause saves to fail if you\nhave the proper indexes in place, or will cause slug lookups to be non-deterministic, either of which is undesirable.\n\n## History\n\n* https://github.com/rails/rails/issues/724\n* https://github.com/rails/rails/issues/5441#issuecomment-4563865\n* https://github.com/rails/rails/issues/4729#issuecomment-5729297\n* https://github.com/rails/rails/issues/5441#issuecomment-264871920\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjbox-web%2Fstore_base_sti_class","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjbox-web%2Fstore_base_sti_class","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjbox-web%2Fstore_base_sti_class/lists"}