Morez SA
Morez SA

Reputation: 87

Update boolean and create new records simultaneously - Rails

I need to update a Boolean and also create new records at the same time. I have a controller called Bank, and Bank has 3 attributes, namely account:string, amount:decimal and withdrawn:Boolean, a customer can invest an amount for a specific number of days, after those days are reached they can then withdraw the amount with interest. I need that when the customer clicks withdraw the withdrawn Boolean is updated to true, and the amount+interest for example amount+10% is created and saved, the amount+10% result will also have a Boolean/flag paid which the “bank teller” can change to paid:, true after the cash payment is done.

How can I achieve the above, should I create a new controller and model for the Withdrawal, and how would that code look like?

Upvotes: 1

Views: 76

Answers (1)

borbesaur
borbesaur

Reputation: 691

No need to create a new controller. Just create a withdraw method in your controller. Not sure exactly what you mean with the amount+interest but I guess you're talking about another column in your database. Just add something like amount_with_interest as an attribute in the database. Also, add another column for paid, as a boolean.

def withdraw
the_bank = Bank.find(params[:id]) #or something similar to get the account one way or another 
the_bank.withdrawn = true 
the_bank.amount_with_interest = the_bank.amount * 1.1
the_bank.save
end

The teller can then press some other button somewhere after the payment has been sent that simply changes the_bank.paid to true

Upvotes: 1

Related Questions