Repository is archived
No commit activity in last 3 years
No release in over 3 years
ActiveRecord has always stored the base class in polymorphic _type columns when using STI. This can have non-trivial performance implications in certain cases. This gem adds 'store_base_sti_class' configuration options which controls whether ActiveRecord will store the base class or the actual class. Default to true for backwards compatibility.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0.0
~> 1.5.2
>= 0
>= 0

Runtime

>= 3.0.5
 Project Readme

Description¶ ↑

Given the following class definitions,

class Address
  belongs_to :addressable, :polymorphic => true
end

class Person
  has_many :addresses, :as => addressable
end

class Vendor < Person
end

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' ...>

Usage¶ ↑

Add the following line to your Gemfile,

gem 'store_base_sti_class_for_3_0'

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.

Notes¶ ↑

The gem has been originally extracted out of github.com/pkmiec/rails/tree/store_base_sti_class_for_3_0_4 patch. It allows the functionality to be used in applications that include Rails as a gem.

Currently, it works with Rails ~> 3.0.5.

This gem will not work with Rails 3.1 as much of its ActiveRecord internals have been replaced with Arel. Similar but different changes need to be applied to Rails 3.1. Those changes will come.

Still using Rails 2.3?¶ ↑

A patch for Rails 2.3 existed which I used until I switched to Rails 3 (sorry, I don’t know the original author of that Rails 2.3 patch). I’ve attached that patch in form of two diffs to this repo. See polymorphic_and_sti_fix_for_rails_2.3-1.diff and polymorphic_and_sti_fix_for_rails_2.3-2.diff. Note, I was using Rails 2.3.4 so YMMV with 2.3.11.

Copyright © 2011 Paul Kmiec. See LICENSE.txt for further details.