Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Modifies ActiveRecord 3.1.x with the ability to store the actual class (instead of the base class) in polymorhic _type columns when using STI
Pull request Compare This branch is 5 commits ahead of appfolio:master.
Latest commit fe01e4c @erich erich Changed creating SpecialPost it has attribute body as compulsory and fix
the version of activerecord to work with either 3.1 or 3.2



Given the following class definitions,

class Address
  belongs_to :addressable, :polymorphic => true

class Person
  has_many :addresses, :as => addressable

class Vendor < Person

and given the following code,

vendor = Vendor.create(...)
address = vendor.addresses.create(...)

p vendor
p address

will output,

#<Vendor id: 1, type: "Vendor" ...>
#<Address id: 1, addressable_id: 1, addressable_type: 'Person' ...>

Notice that addressable_type column is Person even though the actual class is Vendor.

Normally, this isn't a problem, however it can have negative performance characteristic in certain circumstances. The most obvious one is that a join with persons or an extra query is required to find out the actual type of addressable.

This gem add ActiveRecord::Base.store_base_sti_class configuration option. It defaults to true for backwards compatibility. Setting it false will alter ActiveRecord's behavior to store the actual class in polymorphic _type columns when STI is used.

In the example above, if the ActiveRecord::Base.store_base_sti_class is false, the output will be,

#<Vendor id: 1, type: "Vendor" ...>
#<Address id: 1, addressable_id: 1, addressable_type: 'Vendor' ...>


Add the following line to your Gemfile,

gem 'store_base_sti_class_for_3_1'

then bundle install. Once you have the gem installed, add the following to one of the initializers (or make a new one) in config/initializers,

ActiveRecord::Base.store_base_sti_class = false

When changing this behavior you will have write a migration to update all of your existing _type columns accordingly. You may also need to change your application if it explicitly relies on the _type columns.


The gem has used the test cases from, but has been completely rewritten for 3.1. It currently works with ActiveRecord 3.1.0 through 3.1.3.


Copyright © 2011 AppFolio, inc. See LICENSE.txt for further details.

Something went wrong with that request. Please try again.