タグ

stiに関するsh19eのブックマーク (4)

  • [Rails] STI(Single Table Inheritance)でコントローラも一つに纏める | DevelopersIO

    Rails で STI(Single Table Inheritance)を使った時の、コントローラの実装をどうするか?という事について書いてみたいと思います。 STI(単一テーブル継承)とは オブジェクト指向の基概念として、あるクラスを元にサブクラスを定義する継承があります。 この継承は、プログラミング言語ですとと予め機能が提供されていることが多いですが、 RDB ですとテーブル構成によって表現する場合があります。 *1 今回使う STI は、一つのテーブル内に継承関係にあるクラスのカラムを全て持ってしまう方法です。 例として Player クラスを継承した Footballer クラスと Cricketer クラスがあるとします。 親クラスに共通のプロパティ、子クラスがそれぞれのプロパティを持っていますが、これを STI で表現すると以下になります。 テーブルのレコードがどちらの型に

    [Rails] STI(Single Table Inheritance)でコントローラも一つに纏める | DevelopersIO
  • How (and When) to Use Single Table Inheritance in Rails - eugenius

    Last week as I was developing an application to track and analyze ticket and event postings, I came across a design problem. I had first started working on my ticket tracker application as a way to parallel the learning that we were doing in class. I started with a simple scraper that pulled data out of the Stubhub’s client-facing HTML documents, then moved on to writing a simple ORM that saved th

  • Rails: STI(Single Table Inheritance)でハマったところ|TechRacho by BPS株式会社

    最近Rails でSTI を使う機会があったのですが、幾つかハマるポイントがありました。 1. 親クラスを継承したクラスが読み込まれない 継承した子クラスのモデルを親クラスと同じファイルにまとめていると、エラーになる場合があります。 # app/models/user.rb class User < ActiveRecord::Base end class Admin < User end class Guest < User end # rails console で実行 irb(main):001:0> Admin.all NameError: uninitialized constant Admin この問題はRails の自動読み込みの仕組みに関連して起こるようです。 Rails の自動読み込みの話 読み込みされていないクラス/モジュールがあった場合、名前から読み込みするファイルを

    Rails: STI(Single Table Inheritance)でハマったところ|TechRacho by BPS株式会社
  • ActiveRecord::Inheritance

    Single table inheritance Active Record allows inheritance by storing the name of the class in a column that by default is named “type” (can be changed by overwriting Base.inheritance_column). This means that an inheritance looking like this: class Company < ActiveRecord::Base; end class Firm < Company; end class Client < Company; end class PriorityClient < Client; end When you do Firm.create(name:

  • 1