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 Thanks in advance, -- Adrian Madrid HyperX Inc. Mobile: 801.815.1870 Office: 801.566.0670 aemadrid-kSB444ljgzMmlAP/+Wk3EA@public.gmane.org www.hyperxmedia.com 9000 S. 45 W. Sandy, UT 84070 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
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 > endattr is a Symbol, so you need to do self.send(attr) to get the *value* of the attribute. Bye, François
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 >-- Adrian Madrid HyperX Inc. Mobile: 801.815.1870 Office: 801.566.0670 aemadrid-kSB444ljgzMmlAP/+Wk3EA@public.gmane.org www.hyperxmedia.com 9000 S. 45 W. Sandy, UT 84070 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Silly me ! Adrian Madrid said the following on 2005-09-07 16:36:> 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 > endYup, after I read your response, I realized my error. Sorry for the mixup, and good work on your part :) Bye ! François
np and thanks! Adrian Madrid François Beausoleil wrote:> Silly me ! > > Adrian Madrid said the following on 2005-09-07 16:36: > >> 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 > > > Yup, after I read your response, I realized my error. Sorry for the > mixup, and good work on your part :) > > Bye ! > François > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Adrian Madrid HyperX Inc. Mobile: 801.815.1870 Office: 801.566.0670 aemadrid-kSB444ljgzMmlAP/+Wk3EA@public.gmane.org www.hyperxmedia.com 9000 S. 45 W. Sandy, UT 84070 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails