ruby on rails - how to add a attribute value from the last row with another attribute value from current row -


basically have table has attributes: id, created_at, updated_at, user_id, email, debit, credit, , acctbal. use rails, , have add funds button on page has account history. if deposit 50.00 add new row in table , put 50 in deposit attribute keeping credit null. acctbal attribute null, cant figure out how add current credit value last row's acctbal value in table.i using ror. use same technique debit value. create method following there no computation acctbal because stuck.

def create #@account = account.new(account_params) # @account.save # respond_with(@account) @account = account.new(account_params) @account.email = current_user.email @account.user_id = current_user.id respond_to |format|   if @account.save     format.html { redirect_to accounts_url, notice: 'thank , enjoy.' }     format.json { render :show, status: :created, location: @account }   else     format.html { render :new }   format.json { render json: @account.errors, status::unprocessable_entity }   end  end end 

can please me, fingers bleeding trying bunch of stuff ive found online no avail.

i think getting @ this:

def credit   @previous_balance = account.where('user_id = ?', current_user.id).order(:created_at).last.acctbal   @account = account.new(account_params)   @account.email = current_user.email   @account.user_id = current_user.id   @account.acctbal = account_params[:deposit] + @previous_balance    respond_to |format|     if @account.save       format.html { redirect_to accounts_url, notice: 'thank , enjoy.' }       format.json { render :show, status: :created, location: @account }     else       format.html { render :new }       format.json { render json: @account.errors, status::unprocessable_entity }     end   end end 

as said in comments above, pulling last balance last created row id , adding deposit. 2 things note: 1) if way doing it, should never update row in table, or if should use updated_at attribute instead. 2) should place index on created_at attribute keep being drag on db table gets large.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -