# Validation of aggregates

**URL:** <https://www.ruby-forum.com/t/validation-of-aggregates/54418>\
**Category:** Rails\
**Created:** [March 4, 2006, 6:10am UTC](https://www.ruby-forum.com/t/validation-of-aggregates/54418 "2006-03-04T06:10:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [March 4, 2006, 6:10am UTC](https://www.ruby-forum.com/t/validation-of-aggregates/54418/1 "2006-03-04T06:10:04Z")

</div>

This may not be possible…

I have an aggregation of clwholename defined in my model.

the aggregation builds clwholename from :first\_name, middle\_initial,  
last\_name

and it generally works fine.

I am trying to add a validation so while there may be many ‘John’  
first\_names, and ‘Doe’ last\_names, I only want one ‘John D.’

so I tried…

validates\_uniqueness\_of :clwholename

and I get an error when I try to save a new record that it can’t find  
the clwholename column which I thought was the point of using the  
aggregation in the first place.

Is is possible to ‘validate’ on an aggregation?

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [March 4, 2006, 9:59am UTC](https://www.ruby-forum.com/t/validation-of-aggregates/54418/2 "2006-03-04T09:59:16Z")

</div>

Craig W. wrote:

> Is is possible to ‘validate’ on an aggregation?

You’ll have to code both the DB uniqueness check and  
the error adding into a “validate” method.

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

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [March 4, 2006, 3:28pm UTC](https://www.ruby-forum.com/t/validation-of-aggregates/54418/3 "2006-03-04T15:28:54Z")

</div>

On Sat, 2006-03-04 at 19:58 +1100, Mark Reginald J. wrote:

> Craig W. wrote:
> 
> > Is is possible to ‘validate’ on an aggregation?
> 
> You’ll have to code both the DB uniqueness check and  
> the error adding into a “validate” method.

* * *

I’m gathering that’s because the clwholename ‘aggregation’ doesn’t exist  
until the record is written (client.save has completed) and the  
validation must come before.

I think I got it.

thanks

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [March 4, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/validation-of-aggregates/54418/4 "2006-03-04T15:59:08Z")

</div>

On Sun, 2006-03-05 at 01:49 +1100, Mark Reginald J. wrote:

> for uniqueness. You have to do the search on all its component fields.

* * *

that sort of means that an aggregation is not a fully baked proposition  
and perhaps I should just have a column in my table named for the  
aggregation so the aggregation is saved separately - so I can avoid some  
the the gymnastics that I have to do in various places such as  
validation and ‘auto\_complete’

Does this make sense?

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [March 5, 2006, 1:35am UTC](https://www.ruby-forum.com/t/validation-of-aggregates/54418/5 "2006-03-05T01:35:07Z")

</div>

Craig W. wrote:

> that sort of means that an aggregation is not a fully baked proposition  
> and perhaps I should just have a column in my table named for the  
> aggregation so the aggregation is saved separately - so I can avoid some  
> the the gymnastics that I have to do in various places such as  
> validation and ‘auto\_complete’

Yes, full support for validating aggregated attributes is not yet  
in Rails. It could be added, automatically searching for a  
conjunction of components for the uniqueness test, and using to\_s,  
to\_i, or custom methods in the aggregation class to carry out  
other validations.

Stick with your aggregation. Let it handle what it can handle,  
and manually do the other stuff. Even without the aggregated  
name you’d still have to write a custom uniqueness test.

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

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [March 4, 2006, 3:53pm UTC](https://www.ruby-forum.com/t/validation-of-aggregates/54418/6 "2006-03-04T15:53:03Z")

</div>

Craig W. wrote:

> On Sat, 2006-03-04 at 19:58 +1100, Mark Reginald J. wrote:
> 
> > You’ll have to code both the DB uniqueness check and  
> > the error adding into a “validate” method.
> 
> I’m gathering that’s because the clwholename ‘aggregation’ doesn’t exist  
> until the record is written (client.save has completed) and the  
> validation must come before.

No, it exists. It’s just not a database field that can be searched  
for uniqueness. You have to do the search on all its component fields.

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

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [March 5, 2006, 1:41am UTC](https://www.ruby-forum.com/t/validation-of-aggregates/54418/7 "2006-03-05T01:41:10Z")

</div>

On Sun, 2006-03-05 at 11:32 +1100, Mark Reginald J. wrote:

> conjunction of components for the uniqueness test, and using to\_s,  
> to\_i, or custom methods in the aggregation class to carry out  
> other validations.
> 
> Stick with your aggregation. Let it handle what it can handle,  
> and manually do the other stuff. Even without the aggregated  
> name you’d still have to write a custom uniqueness test.

* * *

thank you for the clear explanation

Craig
