# Model Verification

**URL:** <https://www.ruby-forum.com/t/model-verification/134853>\
**Category:** Rails\
**Created:** [April 15, 2008, 5:54pm UTC](https://www.ruby-forum.com/t/model-verification/134853 "2008-04-15T17:54:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 15, 2008, 5:54pm UTC](https://www.ruby-forum.com/t/model-verification/134853/1 "2008-04-15T17:54:56Z")

</div>

Why does this not work?  
validate :must\_exist

def must\_exist  
for team in Team.find(:all)  
errors.add\_to\_base(“Team must exist. Maybe invite them to  
register?”) unless opponent == team.name and home != team.name  
break  
end  
end

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 15, 2008, 6:08pm UTC](https://www.ruby-forum.com/t/model-verification/134853/2 "2008-04-15T18:08:37Z")

</div>

On 15 Apr 2008, at 16:54, edberner wrote:

> end  
> I think it doesn’t work because the logic in the lines above is just  
> fundamentally broken (I can’t make head or tail of what it’s trying to  
> achieve).

say opponent is team C and home is team B, and that you have teams A,  
B, C (and assume find :all returns them in that order).

then on that first iteration you get

errors.add\_to\_base(…) unless C == A and B != A, which is of course  
enough to trigger add\_to\_base.

This would also be horrific if there were lots of teams. From the name  
of your method, don’t you mean

errors.add\_to\_base(…) if Team.find\_by\_name(opponent).nil? ||  
Team.find\_by\_name(home).nil?

Fred
