29th Fri
ActiveRecord::Base.instantiate
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
genki on Fri 29 Dec 2006 at 13:02 with 0 comments