Validation with Aggregation

ActiveRecord supports composed_of for value objects which is
fantastic but one thing that it doesn’t seem to support (or at least
I am unable to find any documentation for) validation of the value

For example, given the following:

class Message < ActiveRecord::Base
composed_of :sender, :class_name => ‘EmailAddress’
composed_of :recipient, :class_name => ‘EmailAddress’


class EmailAddress
attr_reader :name, :address
def initialize(name, address)
@name, @address = name, address


How can I best take advantage of Rails’ validation, given that I’d
like to only specify the validation rules for EmailAddress once. The
validation rules might look something like:

validates_presence_of :name, :address
validates_format_of :email, :with => ADDRESS_FORMAT

I’m guessing the obvious answer is to create an email_addresses table
and use belongs_to but that seems (to me at least) a somewhat heave-
handed approach.



Simon Harris
RedHill Consulting, Pty. Ltd.
12/55-67 Batman Street
West Melbourne VIC Australia 3003
mob: +61 417 505 611
yahoo/msn/skype: haruki_zaemon
gmail: haruki.zaemon
icq: 20461518