{"id":19984625,"url":"https://github.com/twilightcoders/active_record-mti","last_synced_at":"2025-04-10T00:18:58.655Z","repository":{"id":53163085,"uuid":"67847863","full_name":"TwilightCoders/active_record-mti","owner":"TwilightCoders","description":"ActiveRecord support for PostgreSQL's native inherited tables (multi-table inheritance)","archived":false,"fork":false,"pushed_at":"2020-10-17T20:07:09.000Z","size":190,"stargazers_count":97,"open_issues_count":7,"forks_count":8,"subscribers_count":1,"default_branch":"develop","last_synced_at":"2025-04-02T22:06:04.750Z","etag":null,"topics":["activerecord","multi-table-inheritance","postgresql"],"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/TwilightCoders.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}},"created_at":"2016-09-10T03:15:05.000Z","updated_at":"2025-02-08T12:41:55.000Z","dependencies_parsed_at":"2022-08-29T07:42:14.614Z","dependency_job_id":null,"html_url":"https://github.com/TwilightCoders/active_record-mti","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TwilightCoders%2Factive_record-mti","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TwilightCoders%2Factive_record-mti/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TwilightCoders%2Factive_record-mti/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TwilightCoders%2Factive_record-mti/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TwilightCoders","download_url":"https://codeload.github.com/TwilightCoders/active_record-mti/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248131399,"owners_count":21052833,"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":["activerecord","multi-table-inheritance","postgresql"],"created_at":"2024-11-13T04:19:41.800Z","updated_at":"2025-04-10T00:18:58.633Z","avatar_url":"https://github.com/TwilightCoders.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Version      ](https://img.shields.io/gem/v/active_record-mti.svg?maxAge=2592000)](https://rubygems.org/gems/active_record-mti)\n[![Build Status ](https://travis-ci.org/TwilightCoders/active_record-mti.svg)](https://travis-ci.org/TwilightCoders/active_record-mti)\n[![Code Climate ](https://api.codeclimate.com/v1/badges/27b02e09b5da0a7ed2fc/maintainability)](https://codeclimate.com/github/TwilightCoders/active_record-mti/maintainability)\n[![Test Coverage](https://codeclimate.com/github/TwilightCoders/active_record-mti/badges/coverage.svg)](https://codeclimate.com/github/TwilightCoders/active_record-mti/coverage)\n[![Dependencies ](https://badges.depfu.com/badges/172a247844f319b45f01f6e941a0e387/count.svg)](https://depfu.com/github/TwilightCoders/active_record-mti)\n\n# ActiveRecord::MTI\n\nActiveRecord support for PostgreSQL's native inherited tables (multi-table inheritance)\n\n**Supports**\n- Ruby: `2.3`, `2.4`, `2.5`\n- ActiveRecord: `4.2`, `5.0`, `5.1`\n\nConfirmed production use in `4.2`\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'active_record-mti'\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install active_record-mti\n\n## Usage\n\n```ruby\nclass Account \u003c ::ActiveRecord::Base\n  # table_name is 'accounts'\n  # ...\nend\n\nclass User \u003c Account\n  # table_name is 'account/users'\n  # ...\nend\n\nclass Developer \u003c Account\n  # table_name is 'account/developers'\n  # ...\nend\n\nclass Admin \u003c User\n  self.table_name = 'admins'\n  # ...\nend\n\nclass Hacker \u003c Developer\n  # table_name is 'account/developer/hackers'\n  # ...\nend\n```\n\nIn most cases, you shouldn't have to do anything beyond installing the gem. `ActiveRecord::MTI` will do it's best to determine the nature of inheritance in your models. If your models map to their own tables, `ActiveRecord::MTI` will step in and make sure inheritance is treated appropriately. Otherwise it will gracefully acquiesce to `ActiveRecord`'s built-in `STI`. _(see Table Names section below)_.\n\n### Queries\n\n`ActiveRecord` queries work as usual with the following differences:\n\n- The default query of \"\\*\" is changed to include the OID of each row for subclass discrimination. The default select will be `SELECT \"accounts\".\"tableoid\" AS tableoid, \"accounts\".*` (for example)\n\n### Table Names\n\nConventionally—to indicate the nature of inheritance—`ActiveRecord::MTI` expects the `table_name` of a child model to follow the `singular_parent_table_name/plural_child_table_name` pattern. As always, if you need to deviate from this, you can explicitly set the `table_name` as shown below, or configure `ActiveRecord::MTI` using the configure block.\n\nNote, `ActiveRecord::MTI` will fall back on the unnested `table_name` if it's unable to find the nested form, and short of that, it will use the superclass's `table_name`.\n\n### Configuration\n`ActiveRecord::MTI` can be configured using a configure block.\n\n```ruby\n# config/initializers/active_record_mti.rb\n\nActiveRecord::MTI.configure do |config|\n  config.table_name_nesting = true\n  config.nesting_seperator = '/'\n  config.singular_parent = true\nend\n```\n\n### Migrations\n\nIn your migrations define a table to inherit from another table:\n\n```ruby\nclass CreateAccounts \u003c ActiveRecord::Migration\n  def change\n    create_table :accounts do |t|\n      t.jsonb      :settings\n      t.timestamps null: false\n    end\n\n    create_table :users, inherits: :accounts do |t|\n      t.string     :firstname\n      t.string     :lastname\n    end\n\n    create_table :developers, inherits: :users do |t|\n      t.string     :url\n      t.string     :api_key\n    end\n  end\nend\n\n```\n\n### Schema\n\nA schema will be created that reflects the inheritance chain so that `rake:db:schema:load` will work\n\n```ruby\nActiveRecord::Schema.define(version: 20160910024954) do\n\n  create_table \"accounts\", force: :cascade do |t|\n    t.jsonb    \"settings\"\n    t.datetime \"created_at\", null: false\n    t.datetime \"updated_at\", null: false\n  end\n\n  create_table \"users\", inherits: \"accounts\" do |t|\n    t.string \"firstname\"\n    t.string \"lastname\"\n  end\n\n  create_table \"developers\", inherits: \"users\" do |t|\n    t.string \"url\"\n    t.string \"api_key\"\n  end\n\nend\n```\n\n## Contributing\n\n1. Fork it ( https://github.com/TwilightCoders/active_record-mti/fork )\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwilightcoders%2Factive_record-mti","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftwilightcoders%2Factive_record-mti","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwilightcoders%2Factive_record-mti/lists"}