4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

STIのtypeカラムにintegerを使う

Last updated at Posted at 2016-09-22

integerを使いたくなったのでやってみた。

通常のSTI

class Message < ActiveRecord::Base
end

class WarningMessage < Message
end

class ErrorMessage < Message
end

こんな感じにしておくと、messagesテーブルのtypeカラム

  • Message
  • WarningMessage
  • ErrorMessage

という文字列が格納される。

typeをintegerにしたSTI

class Message < ActiveRecord::Base
  TYPE_VALUE = 0

  class << self
    def find_sti_class(type)
      case type
      when WarningMessage::TYPE_VALUE
        WarningMessage
      when ErrorMessage::TYPE_VALUE
        ErrorMessage
      else
        self
      end
    end
    
    def sti_name
      case self
      when WarningMessage
        WarningMessage::TYPE_VALUE
      when ErrorMessage
        ErrorMessage::TYPE_VALUE
      else
        self::TYPE_VALUE
      end
    end 
  end
end

class WarningMessage < Message
  TYPE_VALUE = 1
end

class ErrorMessage < Message
  TYPE_VALUE = 2
end

こんな感じで、find_sti_classsti_nameを変更するとintegerで扱える。

子クラスの情報を知る必要があるのが少しイケていない点ではあるけど…。

gemにしてみた

毎回書くのが面倒なので、gemにしてみた。

sti_type_customizable

これを使うと

class Message < ActiveRecord::Base
  include StiTypeCustomizable
  self.sti_type_value = 0
  sti_child_classes [WarningMessage, ErrorMessage]
end

class WarningMessage < Message
  self.sti_type_value = 1
end

class ErrorMessage < Message
  self.sti_type_value = 2
end

と書くことができる。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?