# How to use validation with aggregation (composed\_of)?

**URL:** <https://www.ruby-forum.com/t/how-to-use-validation-with-aggregation-composed-of/70517>\
**Category:** Rails\
**Created:** [December 17, 2005, 1:47am UTC](https://www.ruby-forum.com/t/how-to-use-validation-with-aggregation-composed-of/70517 "2005-12-17T01:47:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon\_Harris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9dc877/32.png) [@Simon\_Harris](https://www.ruby-forum.com/u/Simon_Harris)\
**Post date:** [December 17, 2005, 1:47am UTC](https://www.ruby-forum.com/t/how-to-use-validation-with-aggregation-composed-of/70517/1 "2005-12-17T01:47:03Z")

</div>

At the risk of being banned for posting the same question twice, I  
thought I’d try once more with a question for the title rather than a  
statement (on the basis that perhaps questions get answered and  
statements ignored 😉 )

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  
objects.

For example, given the following:

class Message \< ActiveRecord::Base  
composed\_of :sender, :class\_name =\> ‘EmailAddress’  
composed\_of :recipient, :class\_name =\> ‘EmailAddress’  
…  
end

class EmailAddress  
attr\_reader :name, :address  
def initialize(name, address)  
@name, @address = name, address  
end  
…  
end

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.

Regards,

Simon

–  
Simon Harris  
RedHill Consulting, Pty. Ltd.  
12/55-67 Batman Street  
West Melbourne VIC Australia 3003  
[http://www.redhillconsulting.com.au](http://www.redhillconsulting.com.au)  
mob: +61 417 505 611  
yahoo/msn/skype: haruki\_zaemon  
gmail: haruki.zaemon  
icq: 20461518

---

<div class="post-metadata">

**Author:** ![Simon\_Harris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9dc877/32.png) [@Simon\_Harris](https://www.ruby-forum.com/u/Simon_Harris)\
**Post date:** [December 17, 2005, 5:29am UTC](https://www.ruby-forum.com/t/how-to-use-validation-with-aggregation-composed-of/70517/2 "2005-12-17T05:29:28Z")

</div>

Simon Harris wrote:

> 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 objects.

Here’s an attempt at a solution. Any better way?

class Message \< ActiveRecord::Base  
composed\_of :sender, :class\_name =\> ‘EmailAddress’,  
:mapping =\> [[:sender\_name], [:sender\_address]]

composed\_of :recipient, :class\_name =\> ‘EmailAddress’,  
:mapping =\> [[:recipient\_name], [:recipient\_address]]

def validate  
[‘sender’, ‘recipient’].each do |attr|  
obj = self.send(attr)  
[‘name’, ‘address’].each do |field|  
msg = obj.send(“valid\_#{field}?”)  
# choose one of the following  
errors.add\_to\_base( “#{attr}'s #{field} #{msg}” ) unless  
msg.blank?  
errors.add( “#{attr}\_#{field}”, msg ) unless msg.blank?  
end  
end  
super  
end  
end

class EmailAddress  
attr\_reader :name, :address

def initialize( name, address )  
@name, @address = name, address  
end

def valid\_address?  
@valid\_address ||=  
check\_valid?( @address ) ? ‘’ : ‘is not a valid email address’  
end

def valid\_name?  
@valid\_name ||=  
if @name.blank?  
‘cannot be blank’  
elsif @name =~ /[A-Za-Z][A-Za-z -]\*/  
‘’  
else  
‘contains invalid characters’  
end  
end  
end

–  
We develop, watch us RoR, in numbers too big to ignore.
