• 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/147

西さん(a.k.a. maiha)のRuby on Rails入門のp112に書かれてる偽STIが面白そうだったので、 instantiateのソースコードをメモ。

# File src/rails-1.1.6/activerecord/lib/active_record/base.rb, line 975
def instantiate(record)
  object = 
    if subclass_name = record[inheritance_column]
      if subclass_name.empty?
        allocate
      else
        require_association_class(subclass_name)
        begin
          compute_type(subclass_name).allocate
        rescue NameError
          raise SubclassNotFound,
            "The single-table inheritance mechanism failed to locate the subclass: '#{ record[inheritance_column] }'. " +
            "This error is raised because the column '#{ inheritance_column }' is reserved for storing the class in case of inheritance. " +
            "Please rename this column if you didn't intend it to be used for storing the inheritance class " +
            "or overwrite #{ self.to_s }.inheritance_column to use another column for that information."
        end
      end
    else
      allocate
    end

  object.instance_variable_set("@attributes", record)
  object
end
ソースコードはここから持ってきました。

This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/147

posted by Png genki on Fri 29 Dec 2006 at 13:02

Comments:

or Preview
Social Bookmarks
  • Delicious
  • B_entry93
  • Clip_16_12_w
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ