Ruby on Rails: attr_accessor for submodels
Posted
by williamjones
on Stack Overflow
See other posts from Stack Overflow
or by williamjones
Published on 2010-04-09T20:30:26Z
Indexed on
2010/04/09
20:33 UTC
Read the original article
Hit count: 299
I'm working with some models where a lot of a given model's key attributes are actually stored in a submodel.
Example:
class WikiArticle
has_many :revisions
has_one :current_revision, :class_name => "Revision", :order => "created_at DESC"
end
class Revision
has_one :wiki_article
end
The Revision class has a ton of database fields, and the WikiArticle has very few. However, I often have to access a Revision's fields from the context of a WikiArticle. The most important case of this is probably on creating an article. I've been doing that with lots of methods that look like this, one for each field:
def description
if @description
@description
elsif current_revision
current_revision.description
else
""
end
end
def description=(string)
@description = string
end
And then on my save, I save @description into a new revision.
This whole thing reminds me a lot of attr_accessor, only it doesn't seem like I can get attr_accessor to do what I need. How can I define an attr_submodel_accessor such that I could just give field names and have it automatically create all those methods the way attr_accessor does?
© Stack Overflow or respective owner