This gem allows you to make models which attributes are distributed by two tables.
Add to your Gemfile:
gem 'activerecord-mti'
Consider the following DB schema:
create_table :subjects do |t|
t.string :name
end
create_table :roles do |t|
t.integer :subject_id
# MTI fields
t.integer :role_id
t.integer :role_type
end
create_table :employees do |t|
t.string :appointment
end
create_table :clients do |t|
t.string :address
end
and corresponding models:
class Subject < ActiveRecord::Base
has_many :roles
end
class Role < ActiveRecord::Base
mti_base
belongs_to :subject
end
class Employee < ActiveRecord::Base
mti_implementation_of :role
end
class Client < ActiveRecord::Base
mti_implementation_of :role
end
Have fun with roles as base and implementation objects at the same time:
Subject.first.roles # => [#<Employee …>, #<Client …>]
Employee.first.subject # => #<User …>
Role.where(role_type: Client).first.address # => String
Client.first.role # => #<Role …>
Client.first.role_id == Client.first.role.id # => true
Client.create!(:address => 'somewhere').role # => #<Role …>
bundle exec rspec spec