Single Table Inheritanceで親子関係にある二つのクラスHoge, Fugaがあるとする。 class Hoge < ActiveRecord::Base end class Fuga < Hoge end このときHogeクラスのオブジェクトaがあったとしてそれをFugaクラスに変換したいとする。 そもそもそういう状況がオブジェクト指向的にどうなのかというのは置いておく。 このとき、 a = Hoge.find(1) b = a.becomes(Fuga) b.save! とすればいいのかと思って試してみるも、ロードしなおしたオブジェクトはHogeクラスのままだった。 オブジェクトを保存するときのクエリーを見ると次のようになっていた。 UPDATE `hoges` SET `type` = 'Fuga', `updated_at` = '2013-04-08 14:0
Rails STI and form_forWritten August 14, 2012. Tagged Ruby on Rails, STI. Rails' form_for conveniently uses "record identification" to figure out the correct URL based on the model: = form_for([:admin, @item]) do |f| = f.text_field :nameIt's also used to figure out the HTTP verb, the field name prefixes, HTML classes and HTML id. So this form would POST to admin_items_path for a new record, or PUT
I am using single table inheritance in my rails application, and want to explicitly set the type of an instance. I have the following; class Event < ActiveRecord::Base class SpecialEvent < Event which is implemented through single table inheritance. SpecialEvent.new works as expected, but I want to be able to do things like Event.new(:type => 'SpecialEvent') So I can create different sub_types eas
I'm using single table inheiritance (STI) to model the foods people eat and use in recipes in SF. STI is a way of representing a class and its subclasses in a single database table. The system defines a WeightPortion is a UserPortion is a Portion and there are a couple of other subclasses too. Rails handles this pretty well in the case that it has loaded all the subclasses into memory and worked o
Rails で違和感を覚えるのは、モデルにだけアプリケーション層に基底クラスが用意されていない所。違和感の正体はMVCの対称性だけではなく、実際に不便なのだ。ここで言う「アプリケーション層の基底クラス」とは、フレームワークレベルに直接定義するのは気がひけるけど、現在のアプリケーションで自分が定義するクラス全体には影響を与えたいような定義を行うクラスの意。例えば、セッション情報であれば、フレームワークレベルでなく、ApplicationControllerに定義するのが自然だし、現在の ActionPack でもそういう実装になっている。同じことがビュー(ヘルパ)においても可能だが、モデルだけはなぜか ActiveRecord::Base を直接継承しており、これが不便な局面が多々ある。 ApplicationModel 具体的には、既存のNKSKプロジェクトのモデル(クラス群)を別のプロジ
After trying to implement this in my project I am realizing just how scant and misleading the docs are on this topic. This thread is more of a request than a tutorial and I am hoping that some of you more experienced Rails folk will fill out the missing pieces of the puzzle.To make your jobs easier, I will cover what I do know...When to use STI: This structure is best used for models that have ide
I am trying to implement a restful app, but I have found some problems with the design, when I have thought in implementig STI. I will try to explain. Only as a example, you may suppose this models Class Person < ActiveRecord::Base has_many :widgets end Class Customer < Person end Class Employee < Person end Class Widget < ActiveRecord::Base belongs_to :person end Then I would like to define these
Because single table inheritance (STI) has been invaluable to me in some recent Rails projects, I thought I’d try to dispel a little of its negative reputation by writing about some reasons you might use it, when to avoid it, and some tips for working with it. If you gave up on Rails’s STI a while ago because it required too many workarounds, I’d recommend giving it another look, as some annoyance
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く