Skip to content

Instantly share code, notes, and snippets.

@mrjjwright
Created April 29, 2011 18:32
Show Gist options
  • Select an option

  • Save mrjjwright/948770 to your computer and use it in GitHub Desktop.

Select an option

Save mrjjwright/948770 to your computer and use it in GitHub Desktop.
Address all or nothing validation
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