Giter Site home page Giter Site logo

skylerrogers / store_base_sti_class Goto Github PK

View Code? Open in Web Editor NEW

This project forked from appfolio/store_base_sti_class

0.0 2.0 0.0 152 KB

Modifies ActiveRecord 3.0.5+ with the ability to store the actual class (instead of the base class) in polymorphic _type columns when using STI

License: MIT License

store_base_sti_class's Introduction

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'

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

This gem incorporates work from:

It currently works with ActiveRecord 3.0.5 through 4.0.1.

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.