All Projects → TwilightCoders → Active_record Mti

TwilightCoders / Active_record Mti

Licence: mit
ActiveRecord support for PostgreSQL's native inherited tables (multi-table inheritance)

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Active record Mti

Pg party
ActiveRecord PostgreSQL Partitioning
Stars: ✭ 294 (+194%)
Mutual labels:  activerecord, postgresql
Rails Pg Extras
Rails PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.
Stars: ✭ 432 (+332%)
Mutual labels:  activerecord, postgresql
Jennifer.cr
Crystal ORM using ActiveRecord pattern with flexible query DSL
Stars: ✭ 309 (+209%)
Mutual labels:  activerecord, postgresql
Torque Postgresql
Add support to complex resources of PostgreSQL, like data types, array associations, and auxiliary statements (CTE)
Stars: ✭ 130 (+30%)
Mutual labels:  activerecord, postgresql
Activerecord Postgis Adapter
ActiveRecord connection adapter for PostGIS, based on postgresql and rgeo
Stars: ✭ 746 (+646%)
Mutual labels:  activerecord, postgresql
Activerecord Postgres enum
Integrate PostgreSQL's enum data type into ActiveRecord's schema and migrations.
Stars: ✭ 227 (+127%)
Mutual labels:  activerecord, postgresql
With advisory lock
Advisory locking for ActiveRecord
Stars: ✭ 409 (+309%)
Mutual labels:  activerecord, postgresql
Querybuilder
SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird
Stars: ✭ 2,111 (+2011%)
Mutual labels:  activerecord, postgresql
Mobility
Pluggable Ruby translation framework
Stars: ✭ 644 (+544%)
Mutual labels:  activerecord, postgresql
Pg search
pg_search builds ActiveRecord named scopes that take advantage of PostgreSQL’s full text search
Stars: ✭ 619 (+519%)
Mutual labels:  activerecord, postgresql
Activerecord Clean Db Structure
Automatic cleanup for the Rails db/structure.sql file (ActiveRecord/PostgreSQL)
Stars: ✭ 101 (+1%)
Mutual labels:  activerecord, postgresql
Ar Uuid
Override migration methods to support UUID columns without having to be explicit about it.
Stars: ✭ 41 (-59%)
Mutual labels:  activerecord, postgresql
Postgresql cursor
ActiveRecord PostgreSQL Adapter extension for using a cursor to return a large result set
Stars: ✭ 384 (+284%)
Mutual labels:  activerecord, postgresql
Openrecord
Make ORMs great again!
Stars: ✭ 474 (+374%)
Mutual labels:  activerecord, postgresql
Monogamy
Add table-level database locking to ActiveRecord
Stars: ✭ 12 (-88%)
Mutual labels:  activerecord, postgresql
Logidze
Database changes log for Rails
Stars: ✭ 1,060 (+960%)
Mutual labels:  activerecord, postgresql
My Cheat Sheets
A place to keep all my cheat sheets for the complete development of ASIC/FPGA hardware or a software app/service.
Stars: ✭ 94 (-6%)
Mutual labels:  postgresql
Od
Česká otevřená data
Stars: ✭ 99 (-1%)
Mutual labels:  postgresql
Leoric
👑 JavaScript ORM for MySQL, PostgreSQL, and SQLite.
Stars: ✭ 94 (-6%)
Mutual labels:  postgresql
Qt Client
This repository contains the source code for the Desktop client. The Desktop client is built using the Qt framework for C++. The client can be extended or customized using JavaScript. This client is used by all editions of xTuple ERP.
Stars: ✭ 93 (-7%)
Mutual labels:  postgresql

Version      Build Status Code Climate Test Coverage Dependencies

ActiveRecord::MTI

ActiveRecord support for PostgreSQL's native inherited tables (multi-table inheritance)

Supports

  • Ruby: 2.3, 2.4, 2.5
  • ActiveRecord: 4.2, 5.0, 5.1

Confirmed production use in 4.2

Installation

Add this line to your application's Gemfile:

gem 'active_record-mti'

And then execute:

$ bundle

Or install it yourself as:

$ gem install active_record-mti

Usage

class Account < ::ActiveRecord::Base
  # table_name is 'accounts'
  # ...
end

class User < Account
  # table_name is 'account/users'
  # ...
end

class Developer < Account
  # table_name is 'account/developers'
  # ...
end

class Admin < User
  self.table_name = 'admins'
  # ...
end

class Hacker < Developer
  # table_name is 'account/developer/hackers'
  # ...
end

In 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).

Queries

ActiveRecord queries work as usual with the following differences:

  • 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)

Table Names

Conventionally—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.

Note, 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.

Configuration

ActiveRecord::MTI can be configured using a configure block.

# config/initializers/active_record_mti.rb

ActiveRecord::MTI.configure do |config|
  config.table_name_nesting = true
  config.nesting_seperator = '/'
  config.singular_parent = true
end

Migrations

In your migrations define a table to inherit from another table:

class CreateAccounts < ActiveRecord::Migration
  def change
    create_table :accounts do |t|
      t.jsonb      :settings
      t.timestamps null: false
    end

    create_table :users, inherits: :accounts do |t|
      t.string     :firstname
      t.string     :lastname
    end

    create_table :developers, inherits: :users do |t|
      t.string     :url
      t.string     :api_key
    end
  end
end

Schema

A schema will be created that reflects the inheritance chain so that rake:db:schema:load will work

ActiveRecord::Schema.define(version: 20160910024954) do

  create_table "accounts", force: :cascade do |t|
    t.jsonb    "settings"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "users", inherits: "accounts" do |t|
    t.string "firstname"
    t.string "lastname"
  end

  create_table "developers", inherits: "users" do |t|
    t.string "url"
    t.string "api_key"
  end

end

Contributing

  1. Fork it ( https://github.com/TwilightCoders/active_record-mti/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].