Created
April 29, 2011 18:32
-
-
Save mrjjwright/948770 to your computer and use it in GitHub Desktop.
Address all or nothing validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Address < ActiveRecord::Base | |
| attr_accessible :address1, :address2, :phone, :city, :state, :zip, :country, :website_url, :name | |
| def validate | |
| required_set = [:address1, :city, :state, :zip, :country] | |
| matches = required_set.map {|my_method| self.send(my_method).present? }.reduce(0) {|a,b| a + (b == true ? 1 : 0) } | |
| self.errors.add(:address1, "Must have a full address!") if matches.between?(1, 4) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment