On 15 Apr 2008, at 16:54, edberner wrote:
>
> 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
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---