特定のモデルの主要な属性の多くが実際にサブモデルに格納されているいくつかのモデルを使用しています。
例:
class WikiArticle
has_many :revisions
has_one :current_revision, :class_name => "Revision", :order => "created_at DESC"
end
class Revision
has_one :wiki_article
end
Revision クラスには大量のデータベース フィールドがあり、WikiArticle にはほとんどありません。ただし、WikiArticle のコンテキストからリビジョンのフィールドにアクセスしなければならないことがよくあります。これの最も重要なケースは、おそらく記事を作成する場合です。私は、フィールドごとに 1 つずつ、次のような多くのメソッドでそれを行ってきました。
def description
if @description
@description
elsif current_revision
current_revision.description
else
""
end
end
def description=(string)
@description = string
end
そして、保存時に @description を新しいリビジョンに保存します。
この全体が私に多くの attr_accessor を思い出させますが、 attr_accessor に必要なことをさせることができないようです。フィールド名を指定するだけで、attr_accessor のようにすべてのメソッドを自動的に作成できるように、attr_submodel_accessor を定義するにはどうすればよいですか?