Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 0.57 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Rails 3. before_destroy validation to prevent deleting parent records
  2. has_one :invoice
  3. before_destroy :check_for_invoice
  4.  
  5. private
  6.  
  7. def check_for_invoice
  8.   unless invoice.nil?
  9.     self.errors[:base] << "Cannot delete shipment while its invoice exists."
  10.   end
  11. end
  12.        
  13. has_one :invoice
  14. before_destroy :check_for_invoice
  15.  
  16. private
  17.  
  18. def check_for_invoice  
  19.   unless invoice.nil?    
  20.     self.errors[:base] << "Cannot delete shipment while its invoice exists."
  21.     return false  
  22.   end
  23. end
  24.        
  25. self.errors[:base] << "Cannot delete shipment while its invoice exists." and return false