Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
Warning Validate the presence of the foreign key, not the instance variable itself. Do this: validates_presence_of :invoice_id Not this: validates_presence_of :invoice If you validate the presence of the associated object, you will get failures on saves when both the parent object and the child object are new. [ show source ] # File vendor/rails/activerecord/lib/active_record/validations.rb, line 512 512: def validates_presence_of(*attr_names) 513: configuration = { :message => ActiveRecord::Errors.default_error_messages[:blank], :on => :save } 514: configuration.update(attr_names.extract_options!) 515: 516: # can't use validates_each here, because it cannot cope with nonexistent attributes, 517: # while errors.add_on_empty can 518: send(validation_method(configuration[:on])) do |record| 519: unless (configuration[:if] && !evaluate_condition(configuration[:if], record)) || (configuration[:unless] && evaluate_condition(configuration[:unless], record)) 520: record.errors.add_on_blank(attr_names, configuration[:message]) 521: end 522: end 523: end
This paste will be private.
From the Design Piracy series on my blog: