Yet Another request to help DRY some code

Basically, I’m looking for a hook that will let me iterate through all
my has_one relationships.

The idea of this class is that each PageAttribute should have exactly
one XX_page_attribute. There are several possibilities for this, so
it’s supposed to be encapsulated by the att_value attribute. (and
attribute that returns and sets the proper XX_page_attribute) the
decision about which XX_page_attribute is based on another column of
PageAttribute: class_name.

any ideas?
thanks,
Jacob

class PageAttribute < ActiveRecord::Base

belongs_to :page

has_one :boolean_page_attribute, :class_name =>
‘BooleanPageAttribute’, :dependent => :destroy
has_one :date_page_attribute, :class_name => ‘DatePageAttribute’,
:dependent => :destroy
has_one :string_page_attribute, :class_name => ‘StringPageAttribute’,
:dependent => :destroy
has_one :page_link_page_attribute, :class_name =>
‘PageLinkPageAttribute’, :dependent => :destroy
has_one :uploaded_file_page_attribute, :class_name =>
‘UploadedFilePageAttribute’, :dependent => :destroy

def att_value
if(self.class_name.to_s == ‘BooleanPageAttribute’)
return self.boolean_page_attribute
end
if(self.class_name.to_s == ‘DatePageAttribute’)
return self.date_page_attribute
end
if(self.class_name.to_s == ‘StringPageAttribute’)
logger.debug “returning string_page_attribute” +
string_page_attribute.to_s
return self.string_page_attribute
end
if(self.class_name.to_s == ‘PageLinkPageAttribute’)
return self.page_link_page_attribute
end
if(self.class_name.to_s == ‘UploadedFilePageAttribute’)
return self.uploaded_file_page_attribute
end
end

def att_value=(val)
logger.debug("setting att_value from: " + val.to_s);

if(self.class_name.to_s == 'BooleanPageAttribute')
  if val.is_a? Hash
    self.boolean_page_attribute = BooleanPageAttribute.new(val)
  else
    self.boolean_page_attribute = val
  end
end
if(self.class_name.to_s == 'DatePageAttribute')
  if val.is_a? Hash
    self.date_page_attribute = DatePageAttribute.new(val)
  else
    self.date_page_attribute = val
  end
end
if(self.class_name.to_s == 'StringPageAttribute')
  if val.is_a? Hash
    self.string_page_attribute = StringPageAttribute.new(val)
  else
    self.string_page_attribute = val
  end
end
if(self.class_name.to_s == 'PageLinkPageAttribute')
  if val.is_a? Hash
    self.page_link_page_attribute = PageLinkPageAttribute.new(val)
  else
    self.page_link_page_attribute = val
  end
end
if(self.class_name.to_s == 'UploadedFilePageAttribute')
  if val.is_a? Hash
    self.uploaded_file_page_attribute = 

UploadedFilePageAttribute.new(val)
else
self.uploaded_file_page_attribute = val
end
end
end

On Nov 7, 2006, at 1:32 PM, Jacob B. wrote:

any ideas?
thanks,
Jacob

Check out ActiveRecord::Reflection#reflect_on_association(:has_one)

http://api.rubyonrails.org/classes/ActiveRecord/Reflection/
ClassMethods.html#M000592

But I wonder if you might not want STI for all the XxxPageAttributes
if there should only be one of them.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

What’s STI? I’d love if I could make DatePageAttribute and
StringPageAttribute simply subclasses of PageAttribute (but use the
table structure I have already). Is this possible? To make a Model
who is backed by a pair of tables?

STI means Single Table Inheritance.

You may want to look into Polymorphic Associations for this particular
case. I recently encountered a similar problem in where using STI was
just too ugly. Polymorphic Associations allow me to subclass out a
“Content” model to a “Page” model and “Article”, etc.

http://wiki.rubyonrails.org/rails/pages/PolymorphicAssociations

Jacob B. wrote:

What’s STI? I’d love if I could make DatePageAttribute and
StringPageAttribute simply subclasses of PageAttribute (but use the
table structure I have already). Is this possible? To make a Model
who is backed by a pair of tables?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs