Guest User

Untitled

a guest
Aug 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. class Invoice < ActiveRecord::Base
  2. validates_presence_of :refnum
  3. validates_presence_of :payer
  4. validates_presence_of :payee
  5. validates_presence_of :total
  6. validates_uniqueness_of :refnum, :scope => :payer
  7.  
  8. before_save :generate_refnum
  9.  
  10. has_many :invoicelines, :dependent => :destroy
  11. belongs_to :payer, :class_name => "User", :foreign_key => "payer"
  12. belongs_to :payee, :class_name => "User", :foreign_key => "payee"
  13. accepts_nested_attributes_for :invoicelines, :allow_destroy => true
  14.  
  15. def generate_refnum
  16. last_id = Invoice.where(:payer => self.payer.id).last.id
  17. self.refnum = last_id + 1 || 1
  18. end
  19. end
Add Comment
Please, Sign In to add comment