{"id":15064579,"url":"https://github.com/appfolio/store_base_sti_class","last_synced_at":"2025-04-08T02:43:00.854Z","repository":{"id":3241556,"uuid":"4278311","full_name":"appfolio/store_base_sti_class","owner":"appfolio","description":"Modifies ActiveRecord 4+ with the ability to store the actual class (instead of the base class) in polymorphic _type columns when using STI","archived":false,"fork":false,"pushed_at":"2024-10-01T18:40:05.000Z","size":197,"stargazers_count":79,"open_issues_count":7,"forks_count":36,"subscribers_count":137,"default_branch":"master","last_synced_at":"2025-04-01T01:37:13.167Z","etag":null,"topics":["rails","ruby"],"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/appfolio.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","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}},"created_at":"2012-05-09T23:45:39.000Z","updated_at":"2025-02-11T12:03:51.000Z","dependencies_parsed_at":"2022-09-12T03:21:40.957Z","dependency_job_id":"f499c3c7-2e8c-4a56-81e1-a710f3a68c04","html_url":"https://github.com/appfolio/store_base_sti_class","commit_stats":{"total_commits":120,"total_committers":18,"mean_commits":6.666666666666667,"dds":0.6583333333333333,"last_synced_commit":"0df213b01ea1cd31652f3eef7855f6956467ddf0"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appfolio%2Fstore_base_sti_class","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appfolio%2Fstore_base_sti_class/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appfolio%2Fstore_base_sti_class/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/appfolio%2Fstore_base_sti_class/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/appfolio","download_url":"https://codeload.github.com/appfolio/store_base_sti_class/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247767232,"owners_count":20992538,"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":["rails","ruby"],"created_at":"2024-09-25T00:21:20.661Z","updated_at":"2025-04-08T02:43:00.831Z","avatar_url":"https://github.com/appfolio.png","language":"Ruby","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","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fappfolio%2Fstore_base_sti_class","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fappfolio%2Fstore_base_sti_class","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fappfolio%2Fstore_base_sti_class/lists"}