association_class
新しいモジュールでメソッドをオーバーライドし、:extend
オプションを使用してメソッドを含めました。また、作業を簡単にするために、整数から文字列へのマッピングハッシュを作成しました。
config/initializers
ディレクトリまたは好きな場所で、ファイルを作成してハッシュを定義し
ますINT_OBJECT_TYPE_TO_CLASSNAME = { 0 => "Project", 1 => "Task", 2 => "Timesheet" }
class CommentObjectType < ActiveRecord::Base
module ClassNamesAsInt
def association_class
return INT_OBJECT_TYPE_TO_CLASSNAME[restricted_object_type].constantize
end
end
end
コメント.rbで
belongs_to :commentable, :polymorphic => true, :extend => CommentObjectType::ClassNamesAsInt