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.
I guess you have a model that looks like this
class Schedule < ActiveRecord::Base
First, you seem to need to add
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:
errors.add(:contractor_zip, 'is invalid') unless contract.zip ==
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
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
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
errors.add(:contract_zip, 'is invalid') unless
contract.applicant.zip == contract_zip
I never really understood attr_accessor until now. Thanks SO much for