2015-02-16

Rails STI type be int

just code
from : http://lorefnon.me/rails/2014/07/27/optimizing-sti-columns.html

class ThirdOrder < ActiveRecord::Base
  ALLOWED_CLASSES = %w(ThirdOrder FamiOrder) #can't Array , it Circular dependency

  #STI addon (parent only)
  def self.find_sti_class type_name
    ALLOWED_CLASSES[type_name.to_i].constantize
  rescue NameError, TypeError
    super
  end
  def self.sti_name
    return ALLOWED_CLASSES.index(self.name)
  end
end
class FamiOrder < ThirdOrder ; end

沒有留言:

張貼留言