Having a decrement (-=) function stop at 0 in Ruby
1 I have the following line of code which very simply decrements a Shareholders number of stocks (integer in the DB) when they sell them to someone else. This works well and as expected. @selling_shareholder.update_attribute(:number_of_stocks, @selling_shareholder.number_of_stocks -= @transaction.number_of_stocks) Very simply what I'd like to do is have the decrement function stop when the number_of_stocks hits 0 i.e. having a negative number should not be possible. I guess I could use a simple unless @selling_shareholder.number_of_stocks > 0 at the end of the line, but I'm wondering if that will actually work without using a loop? ruby-on-rails ruby share | improve this question asked Nov 14 '18 at 18:09 JP89 JP89 104 1 8 2 Riddle me this. How can one have a transaction with a number_of_stocks in excess of their number_of_stocks ? This seems like a simple validation validates :number_of_stocks, numericality: gre...