# Validating a form by verifying two attributes in a related t

**URL:** <https://www.ruby-forum.com/t/validating-a-form-by-verifying-two-attributes-in-a-related-t/114825>\
**Category:** Rails\
**Created:** [September 17, 2007, 9:10pm UTC](https://www.ruby-forum.com/t/validating-a-form-by-verifying-two-attributes-in-a-related-t/114825 "2007-09-17T21:10:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [September 17, 2007, 9:10pm UTC](https://www.ruby-forum.com/t/validating-a-form-by-verifying-two-attributes-in-a-related-t/114825/1 "2007-09-17T21:10:22Z")

</div>

I have a table called Contracts that includes the Name and Contact  
Information for all my Independent Contractors. Each Independent  
Contractor (IC) will have many [work] Schedules. The IC has to submit  
a schedule every week, but I do not want to setup a complicated  
authentication system that is hard to maintain. So, I’d like to have  
a simple form where the ICs must pick their name from a list and  
manually type in their own ZIP code to let the program know it is  
actually them submitting the form.

My question is how can I validate the form by checking that the ZIP  
code matches the name picked (which both reside in my contracts table)  
before allowing the IC to create the schedule? I would imagine I would  
have to create a custom ‘validate’ method in my Schedules model, but  
I’m not sure how to begin.

Cheers,  
Brad

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [September 18, 2007, 1:11am UTC](https://www.ruby-forum.com/t/validating-a-form-by-verifying-two-attributes-in-a-related-t/114825/2 "2007-09-18T01:11:21Z")

</div>

Hi

I guess you have a model that looks like this

class Schedule \< ActiveRecord::Base  
…  
belongs\_to :contract  
…  
end

First, you seem to need to add

attr\_accessor :contractor\_zip

so that you’ll be able to put a field to enter the zip code in the form.

Then you can write your custom validation method:

def validate  
errors.add(:contractor\_zip, ‘is invalid’) unless contract.zip ==  
contractor\_zip  
end

I didn’t test this code, but it should check whether the zip of the  
associated contract is equal to the zip entered in the form. I also  
assume that the drop down with contractor names will populate the  
contract\_id field of the Schedule model.

Hope that helps  
Eugene

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [September 19, 2007, 4:54pm UTC](https://www.ruby-forum.com/t/validating-a-form-by-verifying-two-attributes-in-a-related-t/114825/3 "2007-09-19T16:54:39Z")

</div>

Hi Eugene,

Sorry, I could have offered more information. My tables are actually  
a bit more complicated than I led on, so I had to modify the validate  
method a little bit.

I have 3 tables:  
\*Applicants (has\_one :contract)  
\*Contracts (belongs\_to :applicant)  
\*Schedules (belongs\_to :contract)

Applicants table actually contains the ZIP code (Before they are  
contracted, the apply and contact information is stored in the  
applicants table).

I had to modify the validate method to be able to link through the  
contracts table to my applicants table. It seems to be working  
though!

attr\_accessor :contract\_zip

def validate  
errors.add(:contract\_zip, ‘is invalid’) unless  
contract.applicant.zip == contract\_zip  
end

I never really understood attr\_accessor until now. Thanks SO much for  
your help!
