ruby on rails - How to ensure record is not saved by two users at the same time? -


i have class:

class payment < activerecord::base    attr_accessible :amount, :invoice_id    belongs_to :invoice    validates :amount, :numericality => { :greater_than => 0, :less_than_or_equal_to => :maximum_amount }, :if => "invoice.present?"    private    def maximum_amount     invoice.total if invoice.present?   end  end 

the code above works. how can make sure no 2 users can ever save new payment record @ same time, thereby exceeding invoice total?

is possible @ database level somehow?

thanks help.

you modify maximum_amount function following:

def maximum_amount   if invoice.present?     amt_paid = 0     invoice.payments.each |payment|       amt_paid += payment.amount     end     invoice.total - amt_paid   end end 

this isn't @ database level per say, if second user attempts make payment more existing balance not validate.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -