ruby-on-railsmulti-table-inheritance

Multi Table Inheritance Queries


Setting:

A is a model

B and C are sub types of A (as an MTI relation)

Using this gem to simulate MTI

A.rb

class A < ActiveRecord::Base
  actable
  ...
end 

B.rb

class B < A
  acts_as :A
  ...
end 

C.rb

class C < A
  acts_as :A
  ...
end 

Problem: Queries on type B return on any table entry with parent A, including C.

c = C.create(name, email)

b = B.create(name, email)

B.first # Expected b, actual is c

B.count # Expected 1, actual 2

Solution

  • The README doesn't tell you to inherit from A - it shows the "subclasses" still inheriting directly from ActiveRecord::Base