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