Report abuse

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