Re: Validation help

Adrian Madrid
Wed, 07 Sep 2005 20:36:53 -0000

The thing is if I try your suggestion I get this error:

undefined method `fieldx'' for User:Class

with this code:

validates_each :fieldx do |record, attr|
  record.errors.add attr, ''must be different from fieldy'' if
self.send(attr) == record.fieldy
end

In the end I had to compare record.fieldx with record.fieldy which is an
ugly hack (hard-coding) but it works for this specific case. Though your
idea made me see that I had the record object right there. Now that I
think about it I could do record.send(attr) == record.fieldy

It works! Here''s the final code:

validates_each :fieldx do |record, attr|
  record.errors.add attr, ''must be different from fieldy'' if
record.send(attr) == record.fieldy
end

Thanks for the help,


Adrian Madrid

François Beausoleil wrote:

>
>
> Adrian Madrid said the following on 2005-09-07 13:38:
>
>> What is the best way to validate a field NOT to be the same as
>> another field? I tried doing this unsuccesfully. Any ideas would be
>> apreciated.
>>
>> validates_each :fieldx do |record, attr|
>>     record.errors.add attr, ''must be different from
fieldy'' if attr
>> == fieldy
>> end
>
>
> attr is a Symbol, so you need to do self.send(attr) to get the *value*
> of the attribute.
>
> Bye,
> François
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails